{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "655c3f50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>.container { width:100% !important; }</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.core.display import display, HTML\n",
    "display(HTML(\"<style>.container { width:100% !important; }</style>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "favorite-magnet",
   "metadata": {
    "id": "IU3QDXWZj56H",
    "papermill": {
     "duration": 0.072901,
     "end_time": "2021-04-25T10:51:08.221502",
     "exception": false,
     "start_time": "2021-04-25T10:51:08.148601",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "Phase_1 =  False\n",
    "Phase_2 = False\n",
    "Phase_3 = False\n",
    "Phase_4 = False\n",
    "\n",
    "DATA_FILE = \"sc2021_train_deals.csv\"\n",
    "TARGET = \"target\"\n",
    "\n",
    "# Range of values for selection in phase 1 :\n",
    "import numpy as np\n",
    "N_ESTIMATORS_RANG = list(range(190,250,10)) #list(range(100,1000,10))\n",
    "RANG_LEARNING_RATE =  list(np.arange(0.02,0.04,0.001)) #list(np.arange(0.01,0.1,0.01)) \n",
    "\n",
    "RSCV_N_TRIALS = 100\n",
    "N_TRIALS = 100\n",
    "SEED = 82736\n",
    "\n",
    "# Path definition\n",
    "import pathlib\n",
    "DATA_DIR = pathlib.Path(\".\")\n",
    "LOG_PATH = pathlib.Path(\"./log/\")\n",
    "NAME_RUN_LOG = 'Run_CATBOOST_Logs.log'\n",
    "\n",
    "AGG_COLS = [\"material_code\", \"company_code\", \"country\", \"region\", \"manager_code\"]\n",
    "CAT_COLS = [\"material_code\", \"company_code\",  \"region\",  \"month\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "sharing-stupid",
   "metadata": {
    "papermill": {
     "duration": 0.060715,
     "end_time": "2021-04-25T10:51:08.453016",
     "exception": false,
     "start_time": "2021-04-25T10:51:08.392301",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "params = { \n",
    "    'n_estimators': 208,# 220, # Et1 - 208,# !!! 287 #\n",
    "    'learning_rate': 0.05, # 0.03, # Et1 - 0.05, # !!! 0.021 #\n",
    "    'depth': 4,         #6,\n",
    "    'cat_features': CAT_COLS,\n",
    "    'random_state' : SEED \n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "70422200",
   "metadata": {
    "papermill": {
     "duration": 19.107813,
     "end_time": "2021-04-25T10:51:28.067283",
     "exception": false,
     "start_time": "2021-04-25T10:51:08.959470",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import pathlib\n",
    "import pandas as pd\n",
    "from catboost import CatBoostRegressor\n",
    "from sklearn.metrics import mean_squared_log_error, make_scorer\n",
    "\n",
    "import numpy as np\n",
    "# импортируем класс TimeSeriesSplit \n",
    "# реализующий стратегию перекрестной проверки на временных рядах\n",
    "# и класс RandomizedSearchCV для поиска гиперпараметров\n",
    "from sklearn.model_selection import TimeSeriesSplit\n",
    "from sklearn.model_selection import RandomizedSearchCV\n",
    "from tqdm.notebook import tqdm\n",
    "from time import time\n",
    "import datetime\n",
    "import warnings\n",
    "import dill\n",
    "import math\n",
    "from sklearn.pipeline import Pipeline\n",
    "from optuna.integration import OptunaSearchCV\n",
    "from optuna.distributions import IntUniformDistribution, UniformDistribution\n",
    "\n",
    "warnings.simplefilter(action='ignore', category=FutureWarning)\n",
    "# увеличиваем количество отображаемых столбцов\n",
    "pd.set_option('display.max_columns', 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "72bb1d68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Время старта скрипта: 22-12-2021 16:25\n"
     ]
    }
   ],
   "source": [
    "start_time = time()\n",
    "print('Время старта скрипта: {}'.format(datetime.datetime.now().strftime(\"%d-%m-%Y %H:%M\")))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4ef6b9d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "BASE_LOG = 4 # math.e\n",
    "\n",
    "def exp_b_m1(x, base, r=4):\n",
    "    return np.round(base**x - 1, r)\n",
    "\n",
    "def log_b_1p(x, base):\n",
    "    x.clip(lower=1.0e-200).apply(lambda y: math.log(y+1, base))\n",
    "    return "
   ]
  },
  {
   "cell_type": "raw",
   "id": "87f16c7b",
   "metadata": {},
   "source": [
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.compose import ColumnTransformer\n",
    "from sklearn.model_selection import GridSearchCV,RandomizedSearchCV\n",
    "import optuna\n",
    "from optuna.samplers import RandomSampler\n",
    "from optuna.integration import OptunaSearchCV\n",
    "from optuna.distributions import IntUniformDistribution, UniformDistribution\n",
    "from sklearn.model_selection import cross_val_predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "million-riding",
   "metadata": {
    "id": "_CJf3w1Rj56e",
    "outputId": "c33766b2-a179-4a95-ea16-da37f6ef1116",
    "papermill": {
     "duration": 0.451849,
     "end_time": "2021-04-25T10:51:35.142399",
     "exception": false,
     "start_time": "2021-04-25T10:51:34.690550",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# загружаем набор\n",
    "data = pd.read_csv(DATA_DIR.joinpath(DATA_FILE), parse_dates=[\"month\", \"date\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "72b6c888",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>month</th>\n",
       "      <th>2018-01-01</th>\n",
       "      <th>2018-02-01</th>\n",
       "      <th>2018-03-01</th>\n",
       "      <th>2018-04-01</th>\n",
       "      <th>2018-05-01</th>\n",
       "      <th>2018-06-01</th>\n",
       "      <th>2018-07-01</th>\n",
       "      <th>2018-08-01</th>\n",
       "      <th>2018-09-01</th>\n",
       "      <th>2018-10-01</th>\n",
       "      <th>2018-11-01</th>\n",
       "      <th>2018-12-01</th>\n",
       "      <th>2019-01-01</th>\n",
       "      <th>2019-02-01</th>\n",
       "      <th>2019-03-01</th>\n",
       "      <th>2019-04-01</th>\n",
       "      <th>2019-05-01</th>\n",
       "      <th>2019-06-01</th>\n",
       "      <th>2019-07-01</th>\n",
       "      <th>2019-08-01</th>\n",
       "      <th>2019-09-01</th>\n",
       "      <th>2019-10-01</th>\n",
       "      <th>2019-11-01</th>\n",
       "      <th>2019-12-01</th>\n",
       "      <th>2020-01-01</th>\n",
       "      <th>2020-02-01</th>\n",
       "      <th>2020-03-01</th>\n",
       "      <th>2020-04-01</th>\n",
       "      <th>2020-05-01</th>\n",
       "      <th>2020-06-01</th>\n",
       "      <th>2020-07-01</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>material_code</th>\n",
       "      <th>company_code</th>\n",
       "      <th>country</th>\n",
       "      <th>region</th>\n",
       "      <th>manager_code</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>124</th>\n",
       "      <th>7278</th>\n",
       "      <th>Россия</th>\n",
       "      <th>Респ. Татарстан</th>\n",
       "      <th>17460</th>\n",
       "      <td>340.0</td>\n",
       "      <td>340.0</td>\n",
       "      <td>260.0</td>\n",
       "      <td>240.0</td>\n",
       "      <td>220.0</td>\n",
       "      <td>220.0</td>\n",
       "      <td>220.0</td>\n",
       "      <td>220.0</td>\n",
       "      <td>220.0</td>\n",
       "      <td>280.0</td>\n",
       "      <td>280.0</td>\n",
       "      <td>280.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>185.0</td>\n",
       "      <td>103.0</td>\n",
       "      <td>62.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">133</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">0</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">Белоруссия</th>\n",
       "      <th>Минская обл.</th>\n",
       "      <th>10942</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>60.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>62.0</td>\n",
       "      <td>145.0</td>\n",
       "      <td>124.0</td>\n",
       "      <td>181.0</td>\n",
       "      <td>208.0</td>\n",
       "      <td>207.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>72.0</td>\n",
       "      <td>250.0</td>\n",
       "      <td>394.0</td>\n",
       "      <td>288.0</td>\n",
       "      <td>210.0</td>\n",
       "      <td>249.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Могилевская обл.</th>\n",
       "      <th>10942</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>140.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>220.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>142.0</td>\n",
       "      <td>103.0</td>\n",
       "      <td>145.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>166.0</td>\n",
       "      <td>62.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>г. Минск</th>\n",
       "      <th>10942</th>\n",
       "      <td>0.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>160.0</td>\n",
       "      <td>180.0</td>\n",
       "      <td>99.0</td>\n",
       "      <td>60.0</td>\n",
       "      <td>400.0</td>\n",
       "      <td>120.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>41.0</td>\n",
       "      <td>83.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>42.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Казахстан</th>\n",
       "      <th>г. Нур-Султан</th>\n",
       "      <th>13301</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "month                                                                2018-01-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              340.0   \n",
       "133           0            Белоруссия Минская обл.     10942                0.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301                0.0   \n",
       "\n",
       "month                                                                2018-02-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              340.0   \n",
       "133           0            Белоруссия Минская обл.     10942                0.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942               20.0   \n",
       "                           Казахстан  г. Нур-Султан    13301                0.0   \n",
       "\n",
       "month                                                                2018-03-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              260.0   \n",
       "133           0            Белоруссия Минская обл.     10942                0.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               30.0   \n",
       "\n",
       "month                                                                2018-04-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              240.0   \n",
       "133           0            Белоруссия Минская обл.     10942              200.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               30.0   \n",
       "\n",
       "month                                                                2018-05-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              220.0   \n",
       "133           0            Белоруссия Минская обл.     10942               60.0   \n",
       "                                      Могилевская обл. 10942              140.0   \n",
       "                                      г. Минск         10942               40.0   \n",
       "                           Казахстан  г. Нур-Султан    13301                0.0   \n",
       "\n",
       "month                                                                2018-06-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              220.0   \n",
       "133           0            Белоруссия Минская обл.     10942                0.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942              160.0   \n",
       "                           Казахстан  г. Нур-Султан    13301                0.0   \n",
       "\n",
       "month                                                                2018-07-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              220.0   \n",
       "133           0            Белоруссия Минская обл.     10942                0.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942              180.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               40.0   \n",
       "\n",
       "month                                                                2018-08-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              220.0   \n",
       "133           0            Белоруссия Минская обл.     10942                0.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942               99.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               20.0   \n",
       "\n",
       "month                                                                2018-09-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              220.0   \n",
       "133           0            Белоруссия Минская обл.     10942                0.0   \n",
       "                                      Могилевская обл. 10942              100.0   \n",
       "                                      г. Минск         10942               60.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               40.0   \n",
       "\n",
       "month                                                                2018-10-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              280.0   \n",
       "133           0            Белоруссия Минская обл.     10942                0.0   \n",
       "                                      Могилевская обл. 10942              220.0   \n",
       "                                      г. Минск         10942              400.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               30.0   \n",
       "\n",
       "month                                                                2018-11-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              280.0   \n",
       "133           0            Белоруссия Минская обл.     10942                0.0   \n",
       "                                      Могилевская обл. 10942               20.0   \n",
       "                                      г. Минск         10942              120.0   \n",
       "                           Казахстан  г. Нур-Султан    13301                0.0   \n",
       "\n",
       "month                                                                2018-12-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              280.0   \n",
       "133           0            Белоруссия Минская обл.     10942                0.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942               20.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               40.0   \n",
       "\n",
       "month                                                                2019-01-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              200.0   \n",
       "133           0            Белоруссия Минская обл.     10942                0.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942               40.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               40.0   \n",
       "\n",
       "month                                                                2019-02-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              200.0   \n",
       "133           0            Белоруссия Минская обл.     10942                0.0   \n",
       "                                      Могилевская обл. 10942               80.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               50.0   \n",
       "\n",
       "month                                                                2019-03-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              200.0   \n",
       "133           0            Белоруссия Минская обл.     10942                0.0   \n",
       "                                      Могилевская обл. 10942              142.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301                0.0   \n",
       "\n",
       "month                                                                2019-04-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              185.0   \n",
       "133           0            Белоруссия Минская обл.     10942               36.0   \n",
       "                                      Могилевская обл. 10942              103.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               40.0   \n",
       "\n",
       "month                                                                2019-05-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460              103.0   \n",
       "133           0            Белоруссия Минская обл.     10942               98.0   \n",
       "                                      Могилевская обл. 10942              145.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301                0.0   \n",
       "\n",
       "month                                                                2019-06-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460               62.0   \n",
       "133           0            Белоруссия Минская обл.     10942               82.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               40.0   \n",
       "\n",
       "month                                                                2019-07-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460                0.0   \n",
       "133           0            Белоруссия Минская обл.     10942               62.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               40.0   \n",
       "\n",
       "month                                                                2019-08-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460                0.0   \n",
       "133           0            Белоруссия Минская обл.     10942              145.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942               41.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               40.0   \n",
       "\n",
       "month                                                                2019-09-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460                0.0   \n",
       "133           0            Белоруссия Минская обл.     10942              124.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942               83.0   \n",
       "                           Казахстан  г. Нур-Султан    13301                0.0   \n",
       "\n",
       "month                                                                2019-10-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460                0.0   \n",
       "133           0            Белоруссия Минская обл.     10942              181.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942               82.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               45.0   \n",
       "\n",
       "month                                                                2019-11-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460                0.0   \n",
       "133           0            Белоруссия Минская обл.     10942              208.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942               42.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               50.0   \n",
       "\n",
       "month                                                                2019-12-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460                0.0   \n",
       "133           0            Белоруссия Минская обл.     10942              207.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               45.0   \n",
       "\n",
       "month                                                                2020-01-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460                0.0   \n",
       "133           0            Белоруссия Минская обл.     10942               17.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301                0.0   \n",
       "\n",
       "month                                                                2020-02-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460                0.0   \n",
       "133           0            Белоруссия Минская обл.     10942               72.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               50.0   \n",
       "\n",
       "month                                                                2020-03-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460                0.0   \n",
       "133           0            Белоруссия Минская обл.     10942              250.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               40.0   \n",
       "\n",
       "month                                                                2020-04-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460                0.0   \n",
       "133           0            Белоруссия Минская обл.     10942              394.0   \n",
       "                                      Могилевская обл. 10942              166.0   \n",
       "                                      г. Минск         10942               21.0   \n",
       "                           Казахстан  г. Нур-Султан    13301                0.0   \n",
       "\n",
       "month                                                                2020-05-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460                0.0   \n",
       "133           0            Белоруссия Минская обл.     10942              288.0   \n",
       "                                      Могилевская обл. 10942               62.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301                0.0   \n",
       "\n",
       "month                                                                2020-06-01  \\\n",
       "material_code company_code country    region           manager_code               \n",
       "124           7278         Россия     Респ. Татарстан  17460                0.0   \n",
       "133           0            Белоруссия Минская обл.     10942              210.0   \n",
       "                                      Могилевская обл. 10942                0.0   \n",
       "                                      г. Минск         10942                0.0   \n",
       "                           Казахстан  г. Нур-Султан    13301               50.0   \n",
       "\n",
       "month                                                                2020-07-01  \n",
       "material_code company_code country    region           manager_code              \n",
       "124           7278         Россия     Респ. Татарстан  17460                0.0  \n",
       "133           0            Белоруссия Минская обл.     10942              249.0  \n",
       "                                      Могилевская обл. 10942                0.0  \n",
       "                                      г. Минск         10942                6.0  \n",
       "                           Казахстан  г. Нур-Султан    13301                0.0  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "group_ts = data.groupby(AGG_COLS + [\"month\"])[\"volume\"].sum().unstack(fill_value=0)\n",
    "group_ts.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6779e20d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# \n",
    "def get_features_Cv(df: pd.DataFrame, month: pd.Timestamp, N=1, MDAD_1=2,  MNGR_GRP_MDAD=0) -> pd.DataFrame:\n",
    "    \"\"\"Calculate features for `month`.\"\"\"\n",
    "\n",
    "    start_period = month - pd.offsets.MonthBegin(N)\n",
    "    end_period = month - pd.offsets.MonthBegin(1)\n",
    "\n",
    "    df = df.loc[:, :end_period]\n",
    "\n",
    "    features = pd.DataFrame([], index=df.index)\n",
    "    features[\"month\"] = month.month\n",
    "    # формируем лаги за N месяцев\n",
    "    features[[f\"vol_tm{i}\" for i in range(N, 0, -1)]] = df.loc[:, start_period:end_period].copy()\n",
    " \n",
    "    # Добавление ГРУППОВЫХ скользящих средних\n",
    "    gr = \"country\"\n",
    "    period = 10\n",
    "    df2 = df.copy()\n",
    "    df2[df2.columns.to_list()] = \\\n",
    "                           df2.groupby(level=gr).transform(lambda x: x.mean())\n",
    "    grp_manager_roll_mean = df2.rolling(period, axis=1, min_periods=1)\n",
    "    features = \\\n",
    "        features.join( grp_manager_roll_mean.mean().iloc[:, -1].rename(gr+\"_grp_mean_\"+str(period)))\n",
    "\n",
    "    #  MEAN\n",
    "    for period in [2,3,12]: #range(2,13,1):\n",
    "        rolling = df.rolling(period, axis=1, min_periods=1)\n",
    "        features = features.join(rolling.mean().iloc[:, -1].rename(\"avg_\"+str(period)))\n",
    "\n",
    "    #  median\n",
    "    for period in [2,3,4,12]: # 10,11,12]: #range(2,13,1):\n",
    "        rolling = df.rolling(period, axis=1, min_periods=1)\n",
    "        features = features.join(rolling.median().iloc[:, -1].rename(\"median\"+str(period)))\n",
    "                 \n",
    "    #  MAX\n",
    "    for period in range(2,4,1):\n",
    "        rolling = df.rolling(period, axis=1, min_periods=1)\n",
    "        features = features.join(rolling.max().iloc[:, -1].rename(\"max_\"+str(period)))\n",
    "\n",
    "    features[\"month\"] = month.month\n",
    "\n",
    "    return features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e0bebd8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(DatetimeIndex(['2018-07-01', '2018-08-01', '2018-09-01', '2018-10-01',\n",
       "                '2018-11-01', '2018-12-01', '2019-01-01', '2019-02-01',\n",
       "                '2019-03-01', '2019-04-01', '2019-05-01', '2019-06-01',\n",
       "                '2019-07-01', '2019-08-01', '2019-09-01', '2019-10-01',\n",
       "                '2019-11-01', '2019-12-01', '2020-01-01', '2020-02-01',\n",
       "                '2020-03-01', '2020-04-01', '2020-05-01', '2020-06-01',\n",
       "                '2020-07-01'],\n",
       "               dtype='datetime64[ns]', freq='MS'),\n",
       " 25)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#cv_traine_range = pd.date_range(\"2019-01-01\", \"2020-07-01\", freq=\"MS\")\n",
    "\n",
    "cv_traine_range = pd.date_range(\"2018-07-01\", \"2020-07-01\", freq=\"MS\")\n",
    "cv_traine_range, len(cv_traine_range)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "aabae3ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ac055142ce2b4d1f8ce5a0576c17e556",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/25 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['material_code', 'company_code', 'country', 'region', 'manager_code', 'month', 'vol_tm1', 'country_grp_mean_10', 'avg_2', 'avg_3', 'avg_12', 'median2', 'median3', 'median4', 'median12', 'max_2', 'max_3']\n",
      "Wall time: 19.5 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "full_features = {}\n",
    "\n",
    "tmp = group_ts.clip(lower=1.0e-200).copy()\n",
    "for i in tmp.columns:\n",
    "    tmp[i] = tmp[i].clip(lower=1.0e-200).apply(lambda x: math.log(x+1, BASE_LOG))\n",
    "\n",
    "dataset_features = []\n",
    "for target_month in tqdm(cv_traine_range):\n",
    "    features = get_features_Cv(tmp, target_month)\n",
    "    features[TARGET] = group_ts[target_month]\n",
    "    dataset_features.append(features.reset_index())\n",
    "full_features = pd.concat(dataset_features, ignore_index=True)\n",
    "\n",
    "#CAT_COLS = [\"material_code\", \"company_code\", \"country\", \"region\", \"manager_code\", \"month\"]\n",
    "CAT_COLS = [\"material_code\", \"company_code\",  \"region\",  \"month\"]\n",
    "# создаем список  переменных\n",
    "FTS_COLS = full_features.columns.tolist()\n",
    "FTS_COLS.remove(TARGET)\n",
    "#################################################################################################################################\n",
    "print(FTS_COLS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2c9a27e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['material_code', 'company_code', 'region', 'month', 'vol_tm1', 'country_grp_mean_10', 'avg_2', 'avg_3', 'avg_12', 'median2', 'median3', 'median4', 'median12', 'max_2', 'max_3']\n"
     ]
    }
   ],
   "source": [
    "FTS_COLS.remove(\"country\")\n",
    "FTS_COLS.remove(\"manager_code\")\n",
    "print(FTS_COLS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b9962273",
   "metadata": {},
   "outputs": [],
   "source": [
    "cv_features = full_features.copy()"
   ]
  },
  {
   "cell_type": "raw",
   "id": "5f9f48fa",
   "metadata": {},
   "source": [
    "for c in cv_features.columns:\n",
    "    col_type = cv_features[c].dtype\n",
    "    if col_type == 'object': # or col_type == 'int64': # or col_type.name == 'category':\n",
    "        cv_features[c] = cv_features[c].astype('category')\n",
    "\n",
    "cv_features.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a34be90f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 18 групп - блоки по 1 наблюдению(в 1 наблюдении данные 941 временного ряда)\n",
    "N_timeseries = 941 # в 1 наблюдении данные 941 временного ряда\n",
    "start_train = 1  # \n",
    "\n",
    "test_size=N_timeseries #*3\n",
    "#max_train_size = N_timeseries*6\n",
    "n_splits= 17 #int(len(full_features2)/(test_size))-1 - start_train # \n",
    "\n",
    "tscv = TimeSeriesSplit(n_splits=n_splits, \n",
    "                       #max_train_size=max_train_size, \n",
    "                       test_size=test_size)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "20a0d017",
   "metadata": {},
   "source": [
    "# 3 группы(типа как у организаторов)\n",
    "N_timeseries = 941\n",
    "start_train = 0  # \n",
    "\n",
    "test_size=N_timeseries*6\n",
    "max_train_size = N_timeseries*6\n",
    "n_splits= 2 #int(len(full_features2)/(test_size))-1 - start_train # \n",
    "\n",
    "tscv = TimeSeriesSplit(n_splits=n_splits, \n",
    "                       #max_train_size=max_train_size, \n",
    "                       test_size=test_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "af88c674",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6 групп\n",
    "N_timeseries = 941\n",
    "start_train = 1  # \n",
    "\n",
    "test_size=N_timeseries *3\n",
    "#max_train_size = N_timeseries*6\n",
    "n_splits= 5 #int(len(full_features2)/(test_size))-1 - start_train # \n",
    "\n",
    "tscv = TimeSeriesSplit(n_splits=n_splits, \n",
    "                       #max_train_size=max_train_size, \n",
    "                       test_size=test_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7f6f594d",
   "metadata": {
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TRAIN:  0 ... 9409\n",
      "TEST:  9410 ... 12232\n",
      "Обучающий набор: 10.000000\n",
      "Тестовый набор: 3\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>material_code</th>\n",
       "      <th>company_code</th>\n",
       "      <th>country</th>\n",
       "      <th>region</th>\n",
       "      <th>manager_code</th>\n",
       "      <th>month</th>\n",
       "      <th>vol_tm1</th>\n",
       "      <th>country_grp_mean_10</th>\n",
       "      <th>avg_2</th>\n",
       "      <th>avg_3</th>\n",
       "      <th>avg_12</th>\n",
       "      <th>median2</th>\n",
       "      <th>median3</th>\n",
       "      <th>median4</th>\n",
       "      <th>median12</th>\n",
       "      <th>max_2</th>\n",
       "      <th>max_3</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>9410</th>\n",
       "      <td>124</td>\n",
       "      <td>7278</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Респ. Татарстан</td>\n",
       "      <td>17460</td>\n",
       "      <td>5</td>\n",
       "      <td>3.769579</td>\n",
       "      <td>1.914605</td>\n",
       "      <td>3.797553</td>\n",
       "      <td>3.806877</td>\n",
       "      <td>3.909796</td>\n",
       "      <td>3.797553</td>\n",
       "      <td>3.825526</td>\n",
       "      <td>3.825526</td>\n",
       "      <td>3.893951</td>\n",
       "      <td>3.825526</td>\n",
       "      <td>3.825526</td>\n",
       "      <td>103.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9411</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>Минская обл.</td>\n",
       "      <td>10942</td>\n",
       "      <td>5</td>\n",
       "      <td>2.604727</td>\n",
       "      <td>2.106269</td>\n",
       "      <td>1.302363</td>\n",
       "      <td>0.868242</td>\n",
       "      <td>0.464175</td>\n",
       "      <td>1.302363</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.604727</td>\n",
       "      <td>2.604727</td>\n",
       "      <td>98.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9412</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>Могилевская обл.</td>\n",
       "      <td>10942</td>\n",
       "      <td>5</td>\n",
       "      <td>3.350220</td>\n",
       "      <td>2.106269</td>\n",
       "      <td>3.465078</td>\n",
       "      <td>3.366694</td>\n",
       "      <td>1.924089</td>\n",
       "      <td>3.465078</td>\n",
       "      <td>3.350220</td>\n",
       "      <td>3.260072</td>\n",
       "      <td>2.683042</td>\n",
       "      <td>3.579936</td>\n",
       "      <td>3.579936</td>\n",
       "      <td>145.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9413</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>г. Минск</td>\n",
       "      <td>10942</td>\n",
       "      <td>5</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.106269</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.419962</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.822072</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9414</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Казахстан</td>\n",
       "      <td>г. Нур-Султан</td>\n",
       "      <td>13301</td>\n",
       "      <td>5</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.157494</td>\n",
       "      <td>1.339388</td>\n",
       "      <td>1.838330</td>\n",
       "      <td>1.741946</td>\n",
       "      <td>1.339388</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.577937</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.836213</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12228</th>\n",
       "      <td>986</td>\n",
       "      <td>9943</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Смоленская обл.</td>\n",
       "      <td>17460</td>\n",
       "      <td>7</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.968393</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12229</th>\n",
       "      <td>998</td>\n",
       "      <td>0</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Ленинградская обл.</td>\n",
       "      <td>18079</td>\n",
       "      <td>7</td>\n",
       "      <td>1.729716</td>\n",
       "      <td>1.968393</td>\n",
       "      <td>1.511099</td>\n",
       "      <td>1.535720</td>\n",
       "      <td>0.911504</td>\n",
       "      <td>1.511099</td>\n",
       "      <td>1.584963</td>\n",
       "      <td>1.438722</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>1.729716</td>\n",
       "      <td>1.729716</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12230</th>\n",
       "      <td>998</td>\n",
       "      <td>3380</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Ленинградская обл.</td>\n",
       "      <td>14956</td>\n",
       "      <td>7</td>\n",
       "      <td>3.465369</td>\n",
       "      <td>1.968393</td>\n",
       "      <td>3.482684</td>\n",
       "      <td>3.416765</td>\n",
       "      <td>1.118352</td>\n",
       "      <td>3.482684</td>\n",
       "      <td>3.465369</td>\n",
       "      <td>3.375148</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.500000</td>\n",
       "      <td>3.500000</td>\n",
       "      <td>121.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12231</th>\n",
       "      <td>998</td>\n",
       "      <td>5410</td>\n",
       "      <td>Россия</td>\n",
       "      <td>г. Санкт-Петербург</td>\n",
       "      <td>14956</td>\n",
       "      <td>7</td>\n",
       "      <td>3.329106</td>\n",
       "      <td>1.968393</td>\n",
       "      <td>3.449441</td>\n",
       "      <td>3.452771</td>\n",
       "      <td>3.356213</td>\n",
       "      <td>3.449441</td>\n",
       "      <td>3.459432</td>\n",
       "      <td>3.514604</td>\n",
       "      <td>3.329106</td>\n",
       "      <td>3.569776</td>\n",
       "      <td>3.569776</td>\n",
       "      <td>120.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12232</th>\n",
       "      <td>998</td>\n",
       "      <td>6346</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Респ. Башкортостан</td>\n",
       "      <td>10737</td>\n",
       "      <td>7</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.968393</td>\n",
       "      <td>1.098079</td>\n",
       "      <td>0.732053</td>\n",
       "      <td>0.366026</td>\n",
       "      <td>1.098079</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.098079</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.196159</td>\n",
       "      <td>2.196159</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2823 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       material_code  company_code     country              region  \\\n",
       "9410             124          7278      Россия     Респ. Татарстан   \n",
       "9411             133             0  Белоруссия        Минская обл.   \n",
       "9412             133             0  Белоруссия    Могилевская обл.   \n",
       "9413             133             0  Белоруссия            г. Минск   \n",
       "9414             133             0   Казахстан       г. Нур-Султан   \n",
       "...              ...           ...         ...                 ...   \n",
       "12228            986          9943      Россия     Смоленская обл.   \n",
       "12229            998             0      Россия  Ленинградская обл.   \n",
       "12230            998          3380      Россия  Ленинградская обл.   \n",
       "12231            998          5410      Россия  г. Санкт-Петербург   \n",
       "12232            998          6346      Россия  Респ. Башкортостан   \n",
       "\n",
       "       manager_code  month   vol_tm1  country_grp_mean_10     avg_2     avg_3  \\\n",
       "9410          17460      5  3.769579             1.914605  3.797553  3.806877   \n",
       "9411          10942      5  2.604727             2.106269  1.302363  0.868242   \n",
       "9412          10942      5  3.350220             2.106269  3.465078  3.366694   \n",
       "9413          10942      5  0.000000             2.106269  0.000000  0.000000   \n",
       "9414          13301      5  2.678776             2.157494  1.339388  1.838330   \n",
       "...             ...    ...       ...                  ...       ...       ...   \n",
       "12228         17460      7  0.000000             1.968393  0.000000  0.000000   \n",
       "12229         18079      7  1.729716             1.968393  1.511099  1.535720   \n",
       "12230         14956      7  3.465369             1.968393  3.482684  3.416765   \n",
       "12231         14956      7  3.329106             1.968393  3.449441  3.452771   \n",
       "12232         10737      7  0.000000             1.968393  1.098079  0.732053   \n",
       "\n",
       "         avg_12   median2   median3   median4  median12     max_2     max_3  \\\n",
       "9410   3.909796  3.797553  3.825526  3.825526  3.893951  3.825526  3.825526   \n",
       "9411   0.464175  1.302363  0.000000  0.000000  0.000000  2.604727  2.604727   \n",
       "9412   1.924089  3.465078  3.350220  3.260072  2.683042  3.579936  3.579936   \n",
       "9413   2.419962  0.000000  0.000000  0.000000  2.822072  0.000000  0.000000   \n",
       "9414   1.741946  1.339388  2.678776  2.678776  2.577937  2.678776  2.836213   \n",
       "...         ...       ...       ...       ...       ...       ...       ...   \n",
       "12228  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000   \n",
       "12229  0.911504  1.511099  1.584963  1.438722  1.292481  1.729716  1.729716   \n",
       "12230  1.118352  3.482684  3.465369  3.375148  0.000000  3.500000  3.500000   \n",
       "12231  3.356213  3.449441  3.459432  3.514604  3.329106  3.569776  3.569776   \n",
       "12232  0.366026  1.098079  0.000000  1.098079  0.000000  2.196159  2.196159   \n",
       "\n",
       "       target  \n",
       "9410    103.0  \n",
       "9411     98.0  \n",
       "9412    145.0  \n",
       "9413      0.0  \n",
       "9414      0.0  \n",
       "...       ...  \n",
       "12228     0.0  \n",
       "12229     5.0  \n",
       "12230   121.0  \n",
       "12231   120.0  \n",
       "12232    20.0  \n",
       "\n",
       "[2823 rows x 18 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TRAIN:  0 ... 12232\n",
      "TEST:  12233 ... 15055\n",
      "Обучающий набор: 13.000000\n",
      "Тестовый набор: 3\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>material_code</th>\n",
       "      <th>company_code</th>\n",
       "      <th>country</th>\n",
       "      <th>region</th>\n",
       "      <th>manager_code</th>\n",
       "      <th>month</th>\n",
       "      <th>vol_tm1</th>\n",
       "      <th>country_grp_mean_10</th>\n",
       "      <th>avg_2</th>\n",
       "      <th>avg_3</th>\n",
       "      <th>avg_12</th>\n",
       "      <th>median2</th>\n",
       "      <th>median3</th>\n",
       "      <th>median4</th>\n",
       "      <th>median12</th>\n",
       "      <th>max_2</th>\n",
       "      <th>max_3</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>12233</th>\n",
       "      <td>124</td>\n",
       "      <td>7278</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Респ. Татарстан</td>\n",
       "      <td>17460</td>\n",
       "      <td>8</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.008927</td>\n",
       "      <td>1.494320</td>\n",
       "      <td>2.112953</td>\n",
       "      <td>3.464547</td>\n",
       "      <td>1.494320</td>\n",
       "      <td>2.988640</td>\n",
       "      <td>3.169430</td>\n",
       "      <td>3.825526</td>\n",
       "      <td>2.988640</td>\n",
       "      <td>3.350220</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12234</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>Минская обл.</td>\n",
       "      <td>10942</td>\n",
       "      <td>8</td>\n",
       "      <td>2.988640</td>\n",
       "      <td>2.138527</td>\n",
       "      <td>3.088080</td>\n",
       "      <td>3.163613</td>\n",
       "      <td>1.007964</td>\n",
       "      <td>3.088080</td>\n",
       "      <td>3.187520</td>\n",
       "      <td>3.088080</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.187520</td>\n",
       "      <td>3.314678</td>\n",
       "      <td>145.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12235</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>Могилевская обл.</td>\n",
       "      <td>10942</td>\n",
       "      <td>8</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.138527</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.198304</td>\n",
       "      <td>1.926184</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.675110</td>\n",
       "      <td>2.683042</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.594912</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12236</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>г. Минск</td>\n",
       "      <td>10942</td>\n",
       "      <td>8</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.138527</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.578783</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.098079</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>41.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12237</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Казахстан</td>\n",
       "      <td>г. Нур-Султан</td>\n",
       "      <td>13301</td>\n",
       "      <td>8</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.082592</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>1.785851</td>\n",
       "      <td>1.965177</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>40.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15051</th>\n",
       "      <td>986</td>\n",
       "      <td>9943</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Смоленская обл.</td>\n",
       "      <td>17460</td>\n",
       "      <td>10</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.086452</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15052</th>\n",
       "      <td>998</td>\n",
       "      <td>0</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Ленинградская обл.</td>\n",
       "      <td>18079</td>\n",
       "      <td>10</td>\n",
       "      <td>1.729716</td>\n",
       "      <td>2.086452</td>\n",
       "      <td>1.511099</td>\n",
       "      <td>1.438226</td>\n",
       "      <td>1.066607</td>\n",
       "      <td>1.511099</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>1.511099</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>1.729716</td>\n",
       "      <td>1.729716</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15053</th>\n",
       "      <td>998</td>\n",
       "      <td>3380</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Ленинградская обл.</td>\n",
       "      <td>14956</td>\n",
       "      <td>10</td>\n",
       "      <td>3.441322</td>\n",
       "      <td>2.086452</td>\n",
       "      <td>3.476253</td>\n",
       "      <td>3.472625</td>\n",
       "      <td>1.986508</td>\n",
       "      <td>3.476253</td>\n",
       "      <td>3.465369</td>\n",
       "      <td>3.465369</td>\n",
       "      <td>3.227426</td>\n",
       "      <td>3.511184</td>\n",
       "      <td>3.511184</td>\n",
       "      <td>115.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15054</th>\n",
       "      <td>998</td>\n",
       "      <td>5410</td>\n",
       "      <td>Россия</td>\n",
       "      <td>г. Санкт-Петербург</td>\n",
       "      <td>14956</td>\n",
       "      <td>10</td>\n",
       "      <td>3.169925</td>\n",
       "      <td>2.086452</td>\n",
       "      <td>3.314678</td>\n",
       "      <td>3.362929</td>\n",
       "      <td>3.391199</td>\n",
       "      <td>3.314678</td>\n",
       "      <td>3.459432</td>\n",
       "      <td>3.394269</td>\n",
       "      <td>3.456438</td>\n",
       "      <td>3.459432</td>\n",
       "      <td>3.459432</td>\n",
       "      <td>120.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15055</th>\n",
       "      <td>998</td>\n",
       "      <td>6346</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Респ. Башкортостан</td>\n",
       "      <td>10737</td>\n",
       "      <td>10</td>\n",
       "      <td>2.196159</td>\n",
       "      <td>2.086452</td>\n",
       "      <td>1.098079</td>\n",
       "      <td>1.464106</td>\n",
       "      <td>0.732053</td>\n",
       "      <td>1.098079</td>\n",
       "      <td>2.196159</td>\n",
       "      <td>1.098079</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.196159</td>\n",
       "      <td>2.196159</td>\n",
       "      <td>40.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2823 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       material_code  company_code     country              region  \\\n",
       "12233            124          7278      Россия     Респ. Татарстан   \n",
       "12234            133             0  Белоруссия        Минская обл.   \n",
       "12235            133             0  Белоруссия    Могилевская обл.   \n",
       "12236            133             0  Белоруссия            г. Минск   \n",
       "12237            133             0   Казахстан       г. Нур-Султан   \n",
       "...              ...           ...         ...                 ...   \n",
       "15051            986          9943      Россия     Смоленская обл.   \n",
       "15052            998             0      Россия  Ленинградская обл.   \n",
       "15053            998          3380      Россия  Ленинградская обл.   \n",
       "15054            998          5410      Россия  г. Санкт-Петербург   \n",
       "15055            998          6346      Россия  Респ. Башкортостан   \n",
       "\n",
       "       manager_code  month   vol_tm1  country_grp_mean_10     avg_2     avg_3  \\\n",
       "12233         17460      8  0.000000             2.008927  1.494320  2.112953   \n",
       "12234         10942      8  2.988640             2.138527  3.088080  3.163613   \n",
       "12235         10942      8  0.000000             2.138527  0.000000  1.198304   \n",
       "12236         10942      8  0.000000             2.138527  0.000000  0.000000   \n",
       "12237         13301      8  2.678776             2.082592  2.678776  1.785851   \n",
       "...             ...    ...       ...                  ...       ...       ...   \n",
       "15051         17460     10  0.000000             2.086452  0.000000  0.000000   \n",
       "15052         18079     10  1.729716             2.086452  1.511099  1.438226   \n",
       "15053         14956     10  3.441322             2.086452  3.476253  3.472625   \n",
       "15054         14956     10  3.169925             2.086452  3.314678  3.362929   \n",
       "15055         10737     10  2.196159             2.086452  1.098079  1.464106   \n",
       "\n",
       "         avg_12   median2   median3   median4  median12     max_2     max_3  \\\n",
       "12233  3.464547  1.494320  2.988640  3.169430  3.825526  2.988640  3.350220   \n",
       "12234  1.007964  3.088080  3.187520  3.088080  0.000000  3.187520  3.314678   \n",
       "12235  1.926184  0.000000  0.000000  1.675110  2.683042  0.000000  3.594912   \n",
       "12236  1.578783  0.000000  0.000000  0.000000  1.098079  0.000000  0.000000   \n",
       "12237  1.965177  2.678776  2.678776  2.678776  2.678776  2.678776  2.678776   \n",
       "...         ...       ...       ...       ...       ...       ...       ...   \n",
       "15051  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000   \n",
       "15052  1.066607  1.511099  1.292481  1.511099  1.292481  1.729716  1.729716   \n",
       "15053  1.986508  3.476253  3.465369  3.465369  3.227426  3.511184  3.511184   \n",
       "15054  3.391199  3.314678  3.459432  3.394269  3.456438  3.459432  3.459432   \n",
       "15055  0.732053  1.098079  2.196159  1.098079  0.000000  2.196159  2.196159   \n",
       "\n",
       "       target  \n",
       "12233     0.0  \n",
       "12234   145.0  \n",
       "12235     0.0  \n",
       "12236    41.0  \n",
       "12237    40.0  \n",
       "...       ...  \n",
       "15051     0.0  \n",
       "15052    10.0  \n",
       "15053   115.0  \n",
       "15054   120.0  \n",
       "15055    40.0  \n",
       "\n",
       "[2823 rows x 18 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TRAIN:  0 ... 15055\n",
      "TEST:  15056 ... 17878\n",
      "Обучающий набор: 16.000000\n",
      "Тестовый набор: 3\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>material_code</th>\n",
       "      <th>company_code</th>\n",
       "      <th>country</th>\n",
       "      <th>region</th>\n",
       "      <th>manager_code</th>\n",
       "      <th>month</th>\n",
       "      <th>vol_tm1</th>\n",
       "      <th>country_grp_mean_10</th>\n",
       "      <th>avg_2</th>\n",
       "      <th>avg_3</th>\n",
       "      <th>avg_12</th>\n",
       "      <th>median2</th>\n",
       "      <th>median3</th>\n",
       "      <th>median4</th>\n",
       "      <th>median12</th>\n",
       "      <th>max_2</th>\n",
       "      <th>max_3</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>15056</th>\n",
       "      <td>124</td>\n",
       "      <td>7278</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Респ. Татарстан</td>\n",
       "      <td>17460</td>\n",
       "      <td>11</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.115207</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.476620</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.559900</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15057</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>Минская обл.</td>\n",
       "      <td>10942</td>\n",
       "      <td>11</td>\n",
       "      <td>3.753897</td>\n",
       "      <td>2.305868</td>\n",
       "      <td>3.618395</td>\n",
       "      <td>3.610567</td>\n",
       "      <td>1.910606</td>\n",
       "      <td>3.618395</td>\n",
       "      <td>3.594912</td>\n",
       "      <td>3.538902</td>\n",
       "      <td>2.796683</td>\n",
       "      <td>3.753897</td>\n",
       "      <td>3.753897</td>\n",
       "      <td>208.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15058</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>Могилевская обл.</td>\n",
       "      <td>10942</td>\n",
       "      <td>11</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.305868</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.324263</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15059</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>г. Минск</td>\n",
       "      <td>10942</td>\n",
       "      <td>11</td>\n",
       "      <td>3.187520</td>\n",
       "      <td>2.305868</td>\n",
       "      <td>3.191839</td>\n",
       "      <td>3.026612</td>\n",
       "      <td>1.451184</td>\n",
       "      <td>3.191839</td>\n",
       "      <td>3.187520</td>\n",
       "      <td>2.941839</td>\n",
       "      <td>1.098079</td>\n",
       "      <td>3.196159</td>\n",
       "      <td>3.196159</td>\n",
       "      <td>42.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15060</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Казахстан</td>\n",
       "      <td>г. Нур-Султан</td>\n",
       "      <td>13301</td>\n",
       "      <td>11</td>\n",
       "      <td>2.761781</td>\n",
       "      <td>2.170556</td>\n",
       "      <td>1.380890</td>\n",
       "      <td>1.813519</td>\n",
       "      <td>1.805887</td>\n",
       "      <td>1.380890</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.761781</td>\n",
       "      <td>2.761781</td>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17874</th>\n",
       "      <td>986</td>\n",
       "      <td>9943</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Смоленская обл.</td>\n",
       "      <td>17460</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.171130</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17875</th>\n",
       "      <td>998</td>\n",
       "      <td>0</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Ленинградская обл.</td>\n",
       "      <td>18079</td>\n",
       "      <td>1</td>\n",
       "      <td>1.403677</td>\n",
       "      <td>2.171130</td>\n",
       "      <td>0.701839</td>\n",
       "      <td>1.044464</td>\n",
       "      <td>1.220016</td>\n",
       "      <td>0.701839</td>\n",
       "      <td>1.403677</td>\n",
       "      <td>1.566697</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>1.403677</td>\n",
       "      <td>1.729716</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17876</th>\n",
       "      <td>998</td>\n",
       "      <td>3380</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Ленинградская обл.</td>\n",
       "      <td>14956</td>\n",
       "      <td>1</td>\n",
       "      <td>2.453445</td>\n",
       "      <td>2.171130</td>\n",
       "      <td>2.898348</td>\n",
       "      <td>3.075229</td>\n",
       "      <td>2.755315</td>\n",
       "      <td>2.898348</td>\n",
       "      <td>3.343250</td>\n",
       "      <td>3.386120</td>\n",
       "      <td>3.386120</td>\n",
       "      <td>3.343250</td>\n",
       "      <td>3.428990</td>\n",
       "      <td>73.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17877</th>\n",
       "      <td>998</td>\n",
       "      <td>5410</td>\n",
       "      <td>Россия</td>\n",
       "      <td>г. Санкт-Петербург</td>\n",
       "      <td>14956</td>\n",
       "      <td>1</td>\n",
       "      <td>3.329106</td>\n",
       "      <td>2.171130</td>\n",
       "      <td>3.449441</td>\n",
       "      <td>3.452771</td>\n",
       "      <td>3.448646</td>\n",
       "      <td>3.449441</td>\n",
       "      <td>3.459432</td>\n",
       "      <td>3.394269</td>\n",
       "      <td>3.459432</td>\n",
       "      <td>3.569776</td>\n",
       "      <td>3.569776</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17878</th>\n",
       "      <td>998</td>\n",
       "      <td>6346</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Респ. Башкортостан</td>\n",
       "      <td>10737</td>\n",
       "      <td>1</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.171130</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>1.401747</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.196159</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>40.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2823 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       material_code  company_code     country              region  \\\n",
       "15056            124          7278      Россия     Респ. Татарстан   \n",
       "15057            133             0  Белоруссия        Минская обл.   \n",
       "15058            133             0  Белоруссия    Могилевская обл.   \n",
       "15059            133             0  Белоруссия            г. Минск   \n",
       "15060            133             0   Казахстан       г. Нур-Султан   \n",
       "...              ...           ...         ...                 ...   \n",
       "17874            986          9943      Россия     Смоленская обл.   \n",
       "17875            998             0      Россия  Ленинградская обл.   \n",
       "17876            998          3380      Россия  Ленинградская обл.   \n",
       "17877            998          5410      Россия  г. Санкт-Петербург   \n",
       "17878            998          6346      Россия  Респ. Башкортостан   \n",
       "\n",
       "       manager_code  month   vol_tm1  country_grp_mean_10     avg_2     avg_3  \\\n",
       "15056         17460     11  0.000000             2.115207  0.000000  0.000000   \n",
       "15057         10942     11  3.753897             2.305868  3.618395  3.610567   \n",
       "15058         10942     11  0.000000             2.305868  0.000000  0.000000   \n",
       "15059         10942     11  3.187520             2.305868  3.191839  3.026612   \n",
       "15060         13301     11  2.761781             2.170556  1.380890  1.813519   \n",
       "...             ...    ...       ...                  ...       ...       ...   \n",
       "17874         17460      1  0.000000             2.171130  0.000000  0.000000   \n",
       "17875         18079      1  1.403677             2.171130  0.701839  1.044464   \n",
       "17876         14956      1  2.453445             2.171130  2.898348  3.075229   \n",
       "17877         14956      1  3.329106             2.171130  3.449441  3.452771   \n",
       "17878         10737      1  2.678776             2.171130  2.678776  2.678776   \n",
       "\n",
       "         avg_12   median2   median3   median4  median12     max_2     max_3  \\\n",
       "15056  2.476620  0.000000  0.000000  0.000000  3.559900  0.000000  0.000000   \n",
       "15057  1.910606  3.618395  3.594912  3.538902  2.796683  3.753897  3.753897   \n",
       "15058  1.324263  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000   \n",
       "15059  1.451184  3.191839  3.187520  2.941839  1.098079  3.196159  3.196159   \n",
       "15060  1.805887  1.380890  2.678776  2.678776  2.678776  2.761781  2.761781   \n",
       "...         ...       ...       ...       ...       ...       ...       ...   \n",
       "17874  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000   \n",
       "17875  1.220016  0.701839  1.403677  1.566697  1.292481  1.403677  1.729716   \n",
       "17876  2.755315  2.898348  3.343250  3.386120  3.386120  3.343250  3.428990   \n",
       "17877  3.448646  3.449441  3.459432  3.394269  3.459432  3.569776  3.569776   \n",
       "17878  1.401747  2.678776  2.678776  2.678776  2.196159  2.678776  2.678776   \n",
       "\n",
       "       target  \n",
       "15056     0.0  \n",
       "15057   208.0  \n",
       "15058     0.0  \n",
       "15059    42.0  \n",
       "15060    50.0  \n",
       "...       ...  \n",
       "17874     0.0  \n",
       "17875     5.0  \n",
       "17876    73.0  \n",
       "17877   100.0  \n",
       "17878    40.0  \n",
       "\n",
       "[2823 rows x 18 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TRAIN:  0 ... 17878\n",
      "TEST:  17879 ... 20701\n",
      "Обучающий набор: 19.000000\n",
      "Тестовый набор: 3\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>material_code</th>\n",
       "      <th>company_code</th>\n",
       "      <th>country</th>\n",
       "      <th>region</th>\n",
       "      <th>manager_code</th>\n",
       "      <th>month</th>\n",
       "      <th>vol_tm1</th>\n",
       "      <th>country_grp_mean_10</th>\n",
       "      <th>avg_2</th>\n",
       "      <th>avg_3</th>\n",
       "      <th>avg_12</th>\n",
       "      <th>median2</th>\n",
       "      <th>median3</th>\n",
       "      <th>median4</th>\n",
       "      <th>median12</th>\n",
       "      <th>max_2</th>\n",
       "      <th>max_3</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>17879</th>\n",
       "      <td>124</td>\n",
       "      <td>7278</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Респ. Татарстан</td>\n",
       "      <td>17460</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.146407</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.479958</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17880</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>Минская обл.</td>\n",
       "      <td>10942</td>\n",
       "      <td>2</td>\n",
       "      <td>2.084963</td>\n",
       "      <td>2.440112</td>\n",
       "      <td>2.967591</td>\n",
       "      <td>3.262954</td>\n",
       "      <td>2.726344</td>\n",
       "      <td>2.967591</td>\n",
       "      <td>3.850220</td>\n",
       "      <td>3.802059</td>\n",
       "      <td>3.251099</td>\n",
       "      <td>3.850220</td>\n",
       "      <td>3.853680</td>\n",
       "      <td>72.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17881</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>Могилевская обл.</td>\n",
       "      <td>10942</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.440112</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.141249</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17882</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>г. Минск</td>\n",
       "      <td>10942</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.440112</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.904377</td>\n",
       "      <td>0.982747</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.356566</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.713132</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17883</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Казахстан</td>\n",
       "      <td>г. Нур-Султан</td>\n",
       "      <td>13301</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.293841</td>\n",
       "      <td>1.380890</td>\n",
       "      <td>1.865998</td>\n",
       "      <td>1.825924</td>\n",
       "      <td>1.380890</td>\n",
       "      <td>2.761781</td>\n",
       "      <td>2.761781</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.761781</td>\n",
       "      <td>2.836213</td>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20697</th>\n",
       "      <td>986</td>\n",
       "      <td>9943</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Смоленская обл.</td>\n",
       "      <td>17460</td>\n",
       "      <td>4</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>2.140374</td>\n",
       "      <td>2.614858</td>\n",
       "      <td>1.743239</td>\n",
       "      <td>0.435810</td>\n",
       "      <td>2.614858</td>\n",
       "      <td>2.229716</td>\n",
       "      <td>1.114858</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>125.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20698</th>\n",
       "      <td>998</td>\n",
       "      <td>0</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Ленинградская обл.</td>\n",
       "      <td>18079</td>\n",
       "      <td>4</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>2.140374</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>1.327723</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20699</th>\n",
       "      <td>998</td>\n",
       "      <td>3380</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Ленинградская обл.</td>\n",
       "      <td>14956</td>\n",
       "      <td>4</td>\n",
       "      <td>3.471257</td>\n",
       "      <td>2.140374</td>\n",
       "      <td>3.292833</td>\n",
       "      <td>3.230131</td>\n",
       "      <td>3.298687</td>\n",
       "      <td>3.292833</td>\n",
       "      <td>3.114409</td>\n",
       "      <td>3.109568</td>\n",
       "      <td>3.435156</td>\n",
       "      <td>3.471257</td>\n",
       "      <td>3.471257</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20700</th>\n",
       "      <td>998</td>\n",
       "      <td>5410</td>\n",
       "      <td>Россия</td>\n",
       "      <td>г. Санкт-Петербург</td>\n",
       "      <td>14956</td>\n",
       "      <td>4</td>\n",
       "      <td>3.749923</td>\n",
       "      <td>2.140374</td>\n",
       "      <td>3.749923</td>\n",
       "      <td>3.609651</td>\n",
       "      <td>3.469530</td>\n",
       "      <td>3.749923</td>\n",
       "      <td>3.749923</td>\n",
       "      <td>3.539514</td>\n",
       "      <td>3.459432</td>\n",
       "      <td>3.749923</td>\n",
       "      <td>3.749923</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20701</th>\n",
       "      <td>998</td>\n",
       "      <td>6346</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Респ. Башкортостан</td>\n",
       "      <td>10737</td>\n",
       "      <td>4</td>\n",
       "      <td>2.196159</td>\n",
       "      <td>2.140374</td>\n",
       "      <td>1.098079</td>\n",
       "      <td>1.624978</td>\n",
       "      <td>1.624978</td>\n",
       "      <td>1.098079</td>\n",
       "      <td>2.196159</td>\n",
       "      <td>2.437467</td>\n",
       "      <td>2.196159</td>\n",
       "      <td>2.196159</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>40.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2823 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       material_code  company_code     country              region  \\\n",
       "17879            124          7278      Россия     Респ. Татарстан   \n",
       "17880            133             0  Белоруссия        Минская обл.   \n",
       "17881            133             0  Белоруссия    Могилевская обл.   \n",
       "17882            133             0  Белоруссия            г. Минск   \n",
       "17883            133             0   Казахстан       г. Нур-Султан   \n",
       "...              ...           ...         ...                 ...   \n",
       "20697            986          9943      Россия     Смоленская обл.   \n",
       "20698            998             0      Россия  Ленинградская обл.   \n",
       "20699            998          3380      Россия  Ленинградская обл.   \n",
       "20700            998          5410      Россия  г. Санкт-Петербург   \n",
       "20701            998          6346      Россия  Респ. Башкортостан   \n",
       "\n",
       "       manager_code  month   vol_tm1  country_grp_mean_10     avg_2     avg_3  \\\n",
       "17879         17460      2  0.000000             2.146407  0.000000  0.000000   \n",
       "17880         10942      2  2.084963             2.440112  2.967591  3.262954   \n",
       "17881         10942      2  0.000000             2.440112  0.000000  0.000000   \n",
       "17882         10942      2  0.000000             2.440112  0.000000  0.904377   \n",
       "17883         13301      2  0.000000             2.293841  1.380890  1.865998   \n",
       "...             ...    ...       ...                  ...       ...       ...   \n",
       "20697         17460      4  3.000000             2.140374  2.614858  1.743239   \n",
       "20698         18079      4  1.292481             2.140374  1.292481  1.292481   \n",
       "20699         14956      4  3.471257             2.140374  3.292833  3.230131   \n",
       "20700         14956      4  3.749923             2.140374  3.749923  3.609651   \n",
       "20701         10737      4  2.196159             2.140374  1.098079  1.624978   \n",
       "\n",
       "         avg_12   median2   median3   median4  median12     max_2     max_3  \\\n",
       "17879  1.479958  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000   \n",
       "17880  2.726344  2.967591  3.850220  3.802059  3.251099  3.850220  3.853680   \n",
       "17881  1.141249  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000   \n",
       "17882  0.982747  0.000000  0.000000  1.356566  0.000000  0.000000  2.713132   \n",
       "17883  1.825924  1.380890  2.761781  2.761781  2.678776  2.761781  2.836213   \n",
       "...         ...       ...       ...       ...       ...       ...       ...   \n",
       "20697  0.435810  2.614858  2.229716  1.114858  0.000000  3.000000  3.000000   \n",
       "20698  1.327723  1.292481  1.292481  1.292481  1.292481  1.292481  1.292481   \n",
       "20699  3.298687  3.292833  3.114409  3.109568  3.435156  3.471257  3.471257   \n",
       "20700  3.469530  3.749923  3.749923  3.539514  3.459432  3.749923  3.749923   \n",
       "20701  1.624978  1.098079  2.196159  2.437467  2.196159  2.196159  2.678776   \n",
       "\n",
       "       target  \n",
       "17879     0.0  \n",
       "17880    72.0  \n",
       "17881     0.0  \n",
       "17882     0.0  \n",
       "17883    50.0  \n",
       "...       ...  \n",
       "20697   125.0  \n",
       "20698     0.0  \n",
       "20699   100.0  \n",
       "20700   100.0  \n",
       "20701    40.0  \n",
       "\n",
       "[2823 rows x 18 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TRAIN:  0 ... 20701\n",
      "TEST:  20702 ... 23524\n",
      "Обучающий набор: 22.000000\n",
      "Тестовый набор: 3\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>material_code</th>\n",
       "      <th>company_code</th>\n",
       "      <th>country</th>\n",
       "      <th>region</th>\n",
       "      <th>manager_code</th>\n",
       "      <th>month</th>\n",
       "      <th>vol_tm1</th>\n",
       "      <th>country_grp_mean_10</th>\n",
       "      <th>avg_2</th>\n",
       "      <th>avg_3</th>\n",
       "      <th>avg_12</th>\n",
       "      <th>median2</th>\n",
       "      <th>median3</th>\n",
       "      <th>median4</th>\n",
       "      <th>median12</th>\n",
       "      <th>max_2</th>\n",
       "      <th>max_3</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20702</th>\n",
       "      <td>124</td>\n",
       "      <td>7278</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Респ. Татарстан</td>\n",
       "      <td>17460</td>\n",
       "      <td>5</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.132447</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.528238</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20703</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>Минская обл.</td>\n",
       "      <td>10942</td>\n",
       "      <td>5</td>\n",
       "      <td>4.312854</td>\n",
       "      <td>2.427139</td>\n",
       "      <td>4.149313</td>\n",
       "      <td>3.797846</td>\n",
       "      <td>3.458745</td>\n",
       "      <td>4.149313</td>\n",
       "      <td>3.985772</td>\n",
       "      <td>3.540342</td>\n",
       "      <td>3.538902</td>\n",
       "      <td>4.312854</td>\n",
       "      <td>4.312854</td>\n",
       "      <td>288.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20704</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>Могилевская обл.</td>\n",
       "      <td>10942</td>\n",
       "      <td>5</td>\n",
       "      <td>3.691852</td>\n",
       "      <td>2.427139</td>\n",
       "      <td>1.845926</td>\n",
       "      <td>1.230617</td>\n",
       "      <td>0.607230</td>\n",
       "      <td>1.845926</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.691852</td>\n",
       "      <td>3.691852</td>\n",
       "      <td>62.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20705</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>г. Минск</td>\n",
       "      <td>10942</td>\n",
       "      <td>5</td>\n",
       "      <td>2.229716</td>\n",
       "      <td>2.427139</td>\n",
       "      <td>1.114858</td>\n",
       "      <td>0.743239</td>\n",
       "      <td>1.168557</td>\n",
       "      <td>1.114858</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.229716</td>\n",
       "      <td>2.229716</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20706</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Казахстан</td>\n",
       "      <td>г. Нур-Султан</td>\n",
       "      <td>13301</td>\n",
       "      <td>5</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.090911</td>\n",
       "      <td>1.339388</td>\n",
       "      <td>1.838330</td>\n",
       "      <td>1.825924</td>\n",
       "      <td>1.339388</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>1.339388</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.836213</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23520</th>\n",
       "      <td>986</td>\n",
       "      <td>9943</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Смоленская обл.</td>\n",
       "      <td>17460</td>\n",
       "      <td>7</td>\n",
       "      <td>3.204695</td>\n",
       "      <td>2.068485</td>\n",
       "      <td>3.204695</td>\n",
       "      <td>3.299344</td>\n",
       "      <td>1.260646</td>\n",
       "      <td>3.204695</td>\n",
       "      <td>3.204695</td>\n",
       "      <td>3.204695</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.204695</td>\n",
       "      <td>3.488640</td>\n",
       "      <td>83.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23521</th>\n",
       "      <td>998</td>\n",
       "      <td>0</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Ленинградская обл.</td>\n",
       "      <td>18079</td>\n",
       "      <td>7</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.068485</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>1.110460</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.292481</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23522</th>\n",
       "      <td>998</td>\n",
       "      <td>3380</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Ленинградская обл.</td>\n",
       "      <td>14956</td>\n",
       "      <td>7</td>\n",
       "      <td>2.477098</td>\n",
       "      <td>2.068485</td>\n",
       "      <td>2.238549</td>\n",
       "      <td>2.602068</td>\n",
       "      <td>3.095013</td>\n",
       "      <td>2.238549</td>\n",
       "      <td>2.477098</td>\n",
       "      <td>2.903102</td>\n",
       "      <td>3.336178</td>\n",
       "      <td>2.477098</td>\n",
       "      <td>3.329106</td>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23523</th>\n",
       "      <td>998</td>\n",
       "      <td>5410</td>\n",
       "      <td>Россия</td>\n",
       "      <td>г. Санкт-Петербург</td>\n",
       "      <td>14956</td>\n",
       "      <td>7</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>2.068485</td>\n",
       "      <td>3.124276</td>\n",
       "      <td>3.192552</td>\n",
       "      <td>3.404476</td>\n",
       "      <td>3.124276</td>\n",
       "      <td>3.329106</td>\n",
       "      <td>3.449441</td>\n",
       "      <td>3.459432</td>\n",
       "      <td>3.569776</td>\n",
       "      <td>3.569776</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23524</th>\n",
       "      <td>998</td>\n",
       "      <td>6346</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Респ. Башкортостан</td>\n",
       "      <td>10737</td>\n",
       "      <td>7</td>\n",
       "      <td>2.229716</td>\n",
       "      <td>2.068485</td>\n",
       "      <td>2.212937</td>\n",
       "      <td>2.368217</td>\n",
       "      <td>2.034019</td>\n",
       "      <td>2.212937</td>\n",
       "      <td>2.229716</td>\n",
       "      <td>2.212937</td>\n",
       "      <td>2.212937</td>\n",
       "      <td>2.229716</td>\n",
       "      <td>2.678776</td>\n",
       "      <td>21.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2823 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       material_code  company_code     country              region  \\\n",
       "20702            124          7278      Россия     Респ. Татарстан   \n",
       "20703            133             0  Белоруссия        Минская обл.   \n",
       "20704            133             0  Белоруссия    Могилевская обл.   \n",
       "20705            133             0  Белоруссия            г. Минск   \n",
       "20706            133             0   Казахстан       г. Нур-Султан   \n",
       "...              ...           ...         ...                 ...   \n",
       "23520            986          9943      Россия     Смоленская обл.   \n",
       "23521            998             0      Россия  Ленинградская обл.   \n",
       "23522            998          3380      Россия  Ленинградская обл.   \n",
       "23523            998          5410      Россия  г. Санкт-Петербург   \n",
       "23524            998          6346      Россия  Респ. Башкортостан   \n",
       "\n",
       "       manager_code  month   vol_tm1  country_grp_mean_10     avg_2     avg_3  \\\n",
       "20702         17460      5  0.000000             2.132447  0.000000  0.000000   \n",
       "20703         10942      5  4.312854             2.427139  4.149313  3.797846   \n",
       "20704         10942      5  3.691852             2.427139  1.845926  1.230617   \n",
       "20705         10942      5  2.229716             2.427139  1.114858  0.743239   \n",
       "20706         13301      5  0.000000             2.090911  1.339388  1.838330   \n",
       "...             ...    ...       ...                  ...       ...       ...   \n",
       "23520         17460      7  3.204695             2.068485  3.204695  3.299344   \n",
       "23521         18079      7  1.000000             2.068485  1.000000  0.666667   \n",
       "23522         14956      7  2.477098             2.068485  2.238549  2.602068   \n",
       "23523         14956      7  2.678776             2.068485  3.124276  3.192552   \n",
       "23524         10737      7  2.229716             2.068485  2.212937  2.368217   \n",
       "\n",
       "         avg_12   median2   median3   median4  median12     max_2     max_3  \\\n",
       "20702  0.528238  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000   \n",
       "20703  3.458745  4.149313  3.985772  3.540342  3.538902  4.312854  4.312854   \n",
       "20704  0.607230  1.845926  0.000000  0.000000  0.000000  3.691852  3.691852   \n",
       "20705  1.168557  1.114858  0.000000  0.000000  0.000000  2.229716  2.229716   \n",
       "20706  1.825924  1.339388  2.678776  1.339388  2.678776  2.678776  2.836213   \n",
       "...         ...       ...       ...       ...       ...       ...       ...   \n",
       "23520  1.260646  3.204695  3.204695  3.204695  0.000000  3.204695  3.488640   \n",
       "23521  1.110460  1.000000  1.000000  1.000000  1.292481  1.000000  1.000000   \n",
       "23522  3.095013  2.238549  2.477098  2.903102  3.336178  2.477098  3.329106   \n",
       "23523  3.404476  3.124276  3.329106  3.449441  3.459432  3.569776  3.569776   \n",
       "23524  2.034019  2.212937  2.229716  2.212937  2.212937  2.229716  2.678776   \n",
       "\n",
       "       target  \n",
       "20702     0.0  \n",
       "20703   288.0  \n",
       "20704    62.0  \n",
       "20705     0.0  \n",
       "20706     0.0  \n",
       "...       ...  \n",
       "23520    83.0  \n",
       "23521     9.0  \n",
       "23522    50.0  \n",
       "23523     0.0  \n",
       "23524    21.0  \n",
       "\n",
       "[2823 rows x 18 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tscv.get_n_splits()= 5\n"
     ]
    }
   ],
   "source": [
    "for train_index, test_index in tscv.split(cv_features): # tscv.split(X): # tscv.split(cv_features)\n",
    "    print(\"TRAIN: \", train_index[0],\"...\",train_index[-1])\n",
    "    print(\"TEST: \", test_index[0],\"...\",test_index[-1])\n",
    "\n",
    "    X_train, X_test = cv_features.iloc[train_index], cv_features.iloc[test_index]\n",
    "    y_train, y_test = cv_features.iloc[train_index][TARGET], cv_features.iloc[test_index][TARGET]\n",
    "    \n",
    "    print('Обучающий набор: %f' % (len(X_train)/941)) #test_size))\n",
    "    print('Тестовый набор: %d' % (len(X_test)/941)) #test_size))\n",
    "\n",
    "    #display(X_train)\n",
    "    display(X_test)\n",
    "print(\"tscv.get_n_splits()=\",tscv.get_n_splits())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7051c726",
   "metadata": {
    "id": "5yQ9Qp9Vj56f",
    "outputId": "1488f05d-b4f5-404c-8e95-8056ed6d6c2e",
    "papermill": {
     "duration": 0.092475,
     "end_time": "2021-04-25T10:51:37.605629",
     "exception": false,
     "start_time": "2021-04-25T10:51:37.513154",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['material_code', 'company_code', 'region', 'month']\n",
      "['vol_tm1', 'country_grp_mean_10', 'avg_2', 'avg_3', 'avg_12', 'median2', 'median3', 'median4', 'median12', 'max_2', 'max_3']\n"
     ]
    }
   ],
   "source": [
    "# создаем списки категориальных признаков\n",
    "#cat_columns = data.select_dtypes(include='object').columns\n",
    "cat_columns = CAT_COLS\n",
    "\n",
    "# создаем список количественных признаков\n",
    "#num_columns = train.select_dtypes(exclude='object').columns.tolist()\n",
    "#num_columns =  [x for x in cv_features.columns.to_list() if x not in CAT_COLS + [TARGET]]\n",
    "num_columns =  [x for x in FTS_COLS if x not in CAT_COLS + [TARGET]]\n",
    "print(cat_columns)\n",
    "print(num_columns)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "urban-parker",
   "metadata": {
    "id": "ydsW51MDj56g",
    "papermill": {
     "duration": 0.070321,
     "end_time": "2021-04-25T10:51:37.747471",
     "exception": false,
     "start_time": "2021-04-25T10:51:37.677150",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "> # ********  Phase 1 *************"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2c956d16",
   "metadata": {},
   "outputs": [],
   "source": [
    "# обучаем модель, используя логарифмирование зависимой \n",
    "\n",
    "train = cv_features[FTS_COLS]\n",
    "y_train = cv_features[TARGET].clip(lower=1.0e-200).apply(lambda x: math.log(x+1, BASE_LOG))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "swiss-adoption",
   "metadata": {
    "id": "Z1V4HwqWj56h",
    "papermill": {
     "duration": 0.0832,
     "end_time": "2021-04-25T10:51:38.207045",
     "exception": false,
     "start_time": "2021-04-25T10:51:38.123845",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "if Phase_1:\n",
    "    params2 = params   \n",
    "    # задаем конвейер\n",
    "    ml_pipe = Pipeline([('ml', CatBoostRegressor(**params) )])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "responsible-canvas",
   "metadata": {
    "id": "BNzybeXGj56h",
    "papermill": {
     "duration": 0.082989,
     "end_time": "2021-04-25T10:51:38.362986",
     "exception": false,
     "start_time": "2021-04-25T10:51:38.279997",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "if Phase_1:\n",
    "    # задаем сетку гиперпараметров\n",
    "    param_grid = [{'ml__learning_rate': RANG_LEARNING_RATE,\n",
    "                   'ml__n_estimators' : N_ESTIMATORS_RANG}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "041162e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "rmsle = lambda y_true, y_pred: \\\n",
    "            np.sqrt(mean_squared_log_error(\n",
    "                        np.clip(y_true,0,None), \n",
    "                        np.clip(y_pred,0,None)\n",
    "                    ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fixed-settle",
   "metadata": {
    "id": "toiOlZODj56h",
    "outputId": "4b8b1cc2-3607-4a94-aa12-a9f1b228a10d",
    "papermill": {
     "duration": 0.085583,
     "end_time": "2021-04-25T10:51:38.522242",
     "exception": false,
     "start_time": "2021-04-25T10:51:38.436659",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 0 ns\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "if Phase_1:\n",
    "    # создаем экземпляр класса RandomizedSearchCV или GridSearchCV, передав конвейер,\n",
    "    # сетку гиперпараметров и указав количество\n",
    "    # блоков перекрестной проверки, отключив запись метрик \n",
    "    # для обучающих блоков перекрестной проверки в атрибут cv_results_\n",
    "    #gs = GridSearchCV(\n",
    "    gs = RandomizedSearchCV( \n",
    "                      ml_pipe, \n",
    "                      param_grid, \n",
    "                      cv=tscv,  \n",
    "                      scoring=make_scorer(rmsle, greater_is_better=False),\n",
    "                      n_jobs=-1, \n",
    "                      return_train_score=False,\n",
    "                      verbose=1,\n",
    "                      random_state=SEED,\n",
    "                    n_iter = RSCV_N_TRIALS\n",
    "# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1\n",
    "                     )\n",
    "    # выполняем решетчатый поиск\n",
    "    gs.fit(train, y_train)\n",
    "    \n",
    "    \n",
    "    # записываем оптимальные значения гиперпараметров\n",
    "    OPT_PARAMS_PIPE = gs.best_params_ \n",
    "    display(gs.best_params_, gs.best_score_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "surface-advance",
   "metadata": {
    "id": "wPDat-nmj56i",
    "papermill": {
     "duration": 0.081992,
     "end_time": "2021-04-25T10:51:38.677592",
     "exception": false,
     "start_time": "2021-04-25T10:51:38.595600",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "if Phase_1:\n",
    "    #y_probas = gs.predict_proba(test)\n",
    "    # смотрим наилучшие значения гиперпараметров\n",
    "    print('Наилучшие значения гиперпараметров: {}'.format(OPT_PARAMS_PIPE))\n",
    "    # смотрим наилучшее значение SCORE\n",
    "    print('Наилучшее значение SCORE: {:.3f}'.format(gs.best_score_))\n",
    "    # смотрим значение SCORE на тестовой выборке\n",
    "    #print('Значение SCORE на тестовой выборке: {:.3f}'.format(\n",
    "    #   roc_auc_score(y_test, y_probas[:, 1] )))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "serial-socket",
   "metadata": {
    "id": "zCumPqG-j56i",
    "papermill": {
     "duration": 0.089457,
     "end_time": "2021-04-25T10:51:39.726377",
     "exception": false,
     "start_time": "2021-04-25T10:51:39.636920",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "if Phase_1:\n",
    "    # увеличиваем количество выводимых строк\n",
    "    pd.set_option('display.max_rows', 300)\n",
    "\n",
    "    # выводим результаты\n",
    "    cv_results = pd.DataFrame(gs.cv_results_)[['mean_test_score',\n",
    "                                               'param_ml__n_estimators',\n",
    "                                               'param_ml__learning_rate',\n",
    "                                               ]]\n",
    "    cv_results = cv_results.sort_values(by='mean_test_score', ascending=False).reset_index(drop=True)\n",
    "    for col in cv_results.columns[cv_results.columns.str.contains('enc')]:\n",
    "        if col in 'mean_test_score':\n",
    "            continue\n",
    "        cv_results[col] = cv_results[col].apply(lambda x: type(x).__name__)\n",
    "    display(cv_results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "metallic-fleet",
   "metadata": {
    "id": "1CNKuN-jj56i",
    "papermill": {
     "duration": 0.075618,
     "end_time": "2021-04-25T10:51:40.195204",
     "exception": false,
     "start_time": "2021-04-25T10:51:40.119586",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "> # ********  Phase 2,3,4 *************"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "unlikely-cancellation",
   "metadata": {
    "id": "itTSx5MTj56j",
    "papermill": {
     "duration": 0.085734,
     "end_time": "2021-04-25T10:51:40.355769",
     "exception": false,
     "start_time": "2021-04-25T10:51:40.270035",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "if Phase_2 or Phase_3 or Phase_4:\n",
    "    # задаем итоговый конвейер ПОКА ЧТО С НАЧАЛЬНЫМИ НАСТРОЙКАМИ\n",
    "    ml_pipe2 = Pipeline([('ml', CatBoostRegressor(**params) )])\n",
    "\n",
    "    # УСТАНОВКА В НОВЫЙ PIPE НАИЛУЧШИХ ЗНАЧЕНИЙ НАЙДЕННЫХ В ПРЕДЫДУЩЕЙ ФАЗЕ:  \n",
    "    if Phase_1:  \n",
    "        # присваиваем итоговому конвейеру оптимальные значения гиперпараметров\n",
    "        print('Before modification ml__n_estimators ml_pipe2: ', ml_pipe2.get_params()['ml__n_estimators']) # 'n_estimators'\n",
    "        print('Before modification ml__learning_rate ml_pipe2: ', ml_pipe2.get_params()['ml__learning_rate']) # 'n_estimators'\n",
    "        ml_pipe2.set_params(**OPT_PARAMS_PIPE)\n",
    "        print('After modification ml__n_estimators ml_pipe2: ', ml_pipe2.get_params()['ml__n_estimators'])\n",
    "        print('After modification ml__learning_rate ml_pipe2: ', ml_pipe2.get_params()['ml__learning_rate'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "light-steering",
   "metadata": {
    "papermill": {
     "duration": 0.083289,
     "end_time": "2021-04-25T10:51:40.514563",
     "exception": false,
     "start_time": "2021-04-25T10:51:40.431274",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "params2 = params3 = params4 = {}\n",
    "\n",
    "if Phase_2: \n",
    "    params2 = {\n",
    "        # (1) гиперпараметры, задающие структуру дерева:\n",
    "       'ml__depth': IntUniformDistribution(1, 32),\n",
    "       'ml__num_leaves': IntUniformDistribution(2, 1024),\n",
    "       'ml__max_bin': IntUniformDistribution(1, 1024),  \n",
    "       'ml__min_child_samples': IntUniformDistribution(1, 300),\n",
    "       'ml__min_child_weight': UniformDistribution(0.01, 300)\n",
    "        }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "introductory-courtesy",
   "metadata": {
    "papermill": {
     "duration": 0.085211,
     "end_time": "2021-04-25T10:51:40.675756",
     "exception": false,
     "start_time": "2021-04-25T10:51:40.590545",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    " # (2) гиперпараметры, привносящие случайность в ходе построения дерева:\n",
    "if Phase_3: \n",
    "    params3 = {\n",
    "               'ml__bagging_fraction': UniformDistribution(0.0, 1.0),# синонимы:subsample,...\n",
    "               'ml__colsample_bytree': UniformDistribution(0.0, 1.0), # (0.3, 0.9), #синонимы:colsample_bytree,feature_fraction,...\n",
    "               'ml__subsample': IntUniformDistribution(1, 81, 5), # 'синонимы:subsample',bagging_freq,...\n",
    "             }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "respected-dance",
   "metadata": {
    "papermill": {
     "duration": 0.087288,
     "end_time": "2021-04-25T10:51:40.838808",
     "exception": false,
     "start_time": "2021-04-25T10:51:40.751520",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# (3) гиперпараметры, задающие регуляризацию функции потерь:\n",
    "if Phase_4: \n",
    "    params4 = {\n",
    "                'ml__reg_alpha': UniformDistribution(1e-3, 25.0), #0,01 или 0,1\n",
    "                'ml__reg_lambda': UniformDistribution(1e-3, 25.0), #0,01 или 0,1\n",
    "             }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "sized-minnesota",
   "metadata": {
    "papermill": {
     "duration": 0.08398,
     "end_time": "2021-04-25T10:51:40.998493",
     "exception": false,
     "start_time": "2021-04-25T10:51:40.914513",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "if Phase_2 or Phase_3 or Phase_4:  \n",
    "    # задаем новое пространство поиска\n",
    "    param_distributions = {**params2,**params3,**params4}\n",
    "    print(param_distributions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "russian-matrix",
   "metadata": {
    "id": "VZ8ujEQBj56j",
    "papermill": {
     "duration": 0.086815,
     "end_time": "2021-04-25T10:51:41.160994",
     "exception": false,
     "start_time": "2021-04-25T10:51:41.074179",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "if Phase_2 or Phase_3 or Phase_4:  \n",
    "    # создаем экземпляр класса OptunaSearchCV\n",
    "    optuna_search = OptunaSearchCV(\n",
    "        ml_pipe2,\n",
    "        param_distributions,\n",
    "        scoring=make_scorer(rmsle, greater_is_better=False),\n",
    "        n_trials=N_TRIALS,\n",
    "        n_jobs=-1,\n",
    "        refit=True,\n",
    "        verbose=1,\n",
    "        cv=tscv,   # CV_O,\n",
    "        random_state=SEED)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "sticky-offer",
   "metadata": {
    "id": "3Tbb9_ryj56k",
    "papermill": {
     "duration": 0.087109,
     "end_time": "2021-04-25T10:51:41.326081",
     "exception": false,
     "start_time": "2021-04-25T10:51:41.238972",
     "status": "completed"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 0 ns\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "if Phase_2 or Phase_3 or Phase_4:  \n",
    "    # выполняем оптимизацию\n",
    "    optuna_search.fit(train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "suspected-investor",
   "metadata": {
    "id": "SyreK__Qj56l",
    "papermill": {
     "duration": 0.088597,
     "end_time": "2021-04-25T10:51:41.491593",
     "exception": false,
     "start_time": "2021-04-25T10:51:41.402996",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "if Phase_2 or Phase_3 or Phase_4:  \n",
    "    # записываем оптимальные значения гиперпараметров optuna\n",
    "    opt_optuna_params_pipe = optuna_search.best_params_\n",
    "    if Phase_1:\n",
    "        opt_optuna_params_pipe['ml__learning_rate'] = OPT_PARAMS_PIPE['ml__learning_rate']\n",
    "        opt_optuna_params_pipe['ml__n_estimators'] = OPT_PARAMS_PIPE['ml__n_estimators']\n",
    "\n",
    "    # печатаем наилучшие значения гиперпараметров\n",
    "    print('Наилучшие значения гиперпараметров: {}'.format(opt_optuna_params_pipe))\n",
    "    # печатаем наилучшее значение SCORE\n",
    "    print('Наилучшее значение SCORE: {:.3f}'.format(optuna_search.best_score_))\n",
    "    # смотрим значение SCORE на тестовой выборке\n",
    "    #print('SCORE на тестовой выборке: {:.3f}'.format(\n",
    "    #    rsmle(y_test, optuna_search.predict_proba(test)[:, 1])))\n",
    "    print('')\n",
    "    with open(NAME_RUN_LOG, 'at', encoding='utf-8') as logfile:\n",
    "        print('/nВременная метка окончания ФАЗЫ 2 : {}'.format(datetime.now().strftime(\"%d-%m-%Y %H:%M\")), \n",
    "          file=logfile)\n",
    "        print('Наилучшие значения гиперпараметров найденых optuna: \\n{}'.format(opt_optuna_params_pipe), file=logfile)\n",
    "        print('Наилучшее значение SCORE: {:.3f}'.format(optuna_search.best_score_), file=logfile)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a8644fb",
   "metadata": {},
   "source": [
    "#### Найденные на каждой фазе значения записываем в params = {...} в ячейке вверху ноутбука"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cardiovascular-conservative",
   "metadata": {
    "id": "XEUoguQDj56m",
    "papermill": {
     "duration": 0.074747,
     "end_time": "2021-04-25T10:51:41.642008",
     "exception": false,
     "start_time": "2021-04-25T10:51:41.567261",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "> # ********  Final phase *************"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "mobile-entity",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-04-25T10:51:41.958120Z",
     "iopub.status.busy": "2021-04-25T10:51:41.956950Z",
     "iopub.status.idle": "2021-04-25T10:51:41.964255Z",
     "shell.execute_reply": "2021-04-25T10:51:41.963410Z"
    },
    "id": "iexbKXzIj56m",
    "papermill": {
     "duration": 0.088831,
     "end_time": "2021-04-25T10:51:41.964409",
     "exception": false,
     "start_time": "2021-04-25T10:51:41.875578",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# формируем полный массив меток и массив признаков"
   ]
  },
  {
   "cell_type": "raw",
   "id": "08ad5d65",
   "metadata": {},
   "source": [
    "for c in full_features.columns:\n",
    "    col_type = full_features[c].dtype\n",
    "    if col_type == 'object': # or col_type == 'int64': # or col_type.name == 'category':\n",
    "        full_features[c] = full_features[c].astype('category')\n",
    "full_features.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f9db5e66",
   "metadata": {},
   "outputs": [],
   "source": [
    "fulldata, y_fulldata = full_features[FTS_COLS]  , full_features[TARGET]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "imported-million",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-04-25T10:51:42.124859Z",
     "iopub.status.busy": "2021-04-25T10:51:42.123813Z",
     "iopub.status.idle": "2021-04-25T10:51:42.127414Z",
     "shell.execute_reply": "2021-04-25T10:51:42.126872Z"
    },
    "id": "JEGiSIpDj56m",
    "papermill": {
     "duration": 0.086963,
     "end_time": "2021-04-25T10:51:42.127554",
     "exception": false,
     "start_time": "2021-04-25T10:51:42.040591",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# задаем итоговый конвейер ПОКА ЧТО С НАЧАЛЬНЫМИ НАСТРОЙКАМИ\n",
    "final_ml_pipe=Pipeline([('ml', CatBoostRegressor(**params))])\n",
    "\n",
    "if Phase_1:\n",
    "    final_ml_pipe.set_params(**OPT_PARAMS_PIPE)\n",
    "\n",
    "\n",
    "if Phase_2 or Phase_3 or Phase_4:\n",
    "    # Для контроля изменений смотрим выбранные параметры модели \n",
    "    opt_par = ['ml__n_estimators','ml__learning_rate',\n",
    "               'ml__max_depth','ml__min_child_weight','ml__subsample']\n",
    "    print(opt_par)\n",
    "    print( 'Before modification ml_pipe3: ' ,[final_ml_pipe.get_params()[x] for x in opt_par])\n",
    "    \n",
    "    # УСТАНОВКА В НОВЫЙ PIPE НАИЛУЧШИХ ЗНАЧЕНИЙ НАЙДЕННЫХ В ПРЕДЫДУЩИХ ФАЗАХ:  \n",
    "    # присваиваем итоговому конвейеру оптимальные значения гиперпараметров найденные optuna\n",
    "    final_ml_pipe.set_params(**opt_optuna_params_pipe)\n",
    "    \n",
    "    # Для контроля изменений\n",
    "    print( 'After modification ml_pipe3: ' ,[final_ml_pipe.get_params()[x] for x in opt_par])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "e3f311c7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-04-25T10:51:42.617688Z",
     "iopub.status.busy": "2021-04-25T10:51:42.616671Z",
     "iopub.status.idle": "2021-04-25T10:51:42.620462Z",
     "shell.execute_reply": "2021-04-25T10:51:42.619910Z"
    },
    "papermill": {
     "duration": 0.086094,
     "end_time": "2021-04-25T10:51:42.620632",
     "exception": false,
     "start_time": "2021-04-25T10:51:42.534538",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ml__n_estimators': 217}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Нужно увеличить n_estimators, ЕСЛИ параметры были найдены на уменьшенном наборе данных\n",
    "CV = 24 \n",
    "par={}\n",
    "\n",
    "par['ml__n_estimators'] = int(final_ml_pipe.get_params()['ml__n_estimators'] * (1 + 1/(CV-1)))\n",
    "final_ml_pipe.set_params(**par )\n",
    "\n",
    "par"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "comfortable-shakespeare",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-04-25T10:51:43.724036Z",
     "iopub.status.busy": "2021-04-25T10:51:43.722853Z",
     "iopub.status.idle": "2021-04-25T10:52:54.365689Z",
     "shell.execute_reply": "2021-04-25T10:52:54.364505Z"
    },
    "id": "L_w96b7Sj56n",
    "papermill": {
     "duration": 70.74439,
     "end_time": "2021-04-25T10:52:54.365928",
     "exception": false,
     "start_time": "2021-04-25T10:51:43.621538",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:\tlearn: 1.5853294\ttotal: 172ms\tremaining: 37.2s\n",
      "1:\tlearn: 1.5498679\ttotal: 203ms\tremaining: 21.8s\n",
      "2:\tlearn: 1.5174013\ttotal: 246ms\tremaining: 17.6s\n",
      "3:\tlearn: 1.4869919\ttotal: 278ms\tremaining: 14.8s\n",
      "4:\tlearn: 1.4588294\ttotal: 308ms\tremaining: 13.1s\n",
      "5:\tlearn: 1.4324457\ttotal: 352ms\tremaining: 12.4s\n",
      "6:\tlearn: 1.4083945\ttotal: 392ms\tremaining: 11.7s\n",
      "7:\tlearn: 1.3860031\ttotal: 425ms\tremaining: 11.1s\n",
      "8:\tlearn: 1.3654851\ttotal: 463ms\tremaining: 10.7s\n",
      "9:\tlearn: 1.3463234\ttotal: 496ms\tremaining: 10.3s\n",
      "10:\tlearn: 1.3289518\ttotal: 531ms\tremaining: 9.95s\n",
      "11:\tlearn: 1.3132769\ttotal: 571ms\tremaining: 9.75s\n",
      "12:\tlearn: 1.2987792\ttotal: 611ms\tremaining: 9.58s\n",
      "13:\tlearn: 1.2852632\ttotal: 646ms\tremaining: 9.36s\n",
      "14:\tlearn: 1.2727303\ttotal: 683ms\tremaining: 9.2s\n",
      "15:\tlearn: 1.2609570\ttotal: 721ms\tremaining: 9.05s\n",
      "16:\tlearn: 1.2501054\ttotal: 759ms\tremaining: 8.93s\n",
      "17:\tlearn: 1.2401002\ttotal: 796ms\tremaining: 8.8s\n",
      "18:\tlearn: 1.2311314\ttotal: 838ms\tremaining: 8.74s\n",
      "19:\tlearn: 1.2229992\ttotal: 877ms\tremaining: 8.64s\n",
      "20:\tlearn: 1.2153625\ttotal: 920ms\tremaining: 8.58s\n",
      "21:\tlearn: 1.2082227\ttotal: 952ms\tremaining: 8.44s\n",
      "22:\tlearn: 1.2016838\ttotal: 984ms\tremaining: 8.3s\n",
      "23:\tlearn: 1.1956557\ttotal: 1.01s\tremaining: 8.16s\n",
      "24:\tlearn: 1.1902917\ttotal: 1.05s\tremaining: 8.06s\n",
      "25:\tlearn: 1.1849838\ttotal: 1.09s\tremaining: 8s\n",
      "26:\tlearn: 1.1803970\ttotal: 1.12s\tremaining: 7.89s\n",
      "27:\tlearn: 1.1759813\ttotal: 1.16s\tremaining: 7.8s\n",
      "28:\tlearn: 1.1721899\ttotal: 1.19s\tremaining: 7.7s\n",
      "29:\tlearn: 1.1685717\ttotal: 1.22s\tremaining: 7.58s\n",
      "30:\tlearn: 1.1647615\ttotal: 1.25s\tremaining: 7.49s\n",
      "31:\tlearn: 1.1615173\ttotal: 1.28s\tremaining: 7.43s\n",
      "32:\tlearn: 1.1584885\ttotal: 1.32s\tremaining: 7.35s\n",
      "33:\tlearn: 1.1559314\ttotal: 1.35s\tremaining: 7.25s\n",
      "34:\tlearn: 1.1534349\ttotal: 1.38s\tremaining: 7.16s\n",
      "35:\tlearn: 1.1511309\ttotal: 1.41s\tremaining: 7.08s\n",
      "36:\tlearn: 1.1489990\ttotal: 1.44s\tremaining: 7s\n",
      "37:\tlearn: 1.1470949\ttotal: 1.47s\tremaining: 6.92s\n",
      "38:\tlearn: 1.1452561\ttotal: 1.5s\tremaining: 6.86s\n",
      "39:\tlearn: 1.1434970\ttotal: 1.54s\tremaining: 6.81s\n",
      "40:\tlearn: 1.1420324\ttotal: 1.57s\tremaining: 6.75s\n",
      "41:\tlearn: 1.1407877\ttotal: 1.6s\tremaining: 6.69s\n",
      "42:\tlearn: 1.1395770\ttotal: 1.64s\tremaining: 6.62s\n",
      "43:\tlearn: 1.1382035\ttotal: 1.66s\tremaining: 6.54s\n",
      "44:\tlearn: 1.1370776\ttotal: 1.69s\tremaining: 6.47s\n",
      "45:\tlearn: 1.1359188\ttotal: 1.73s\tremaining: 6.42s\n",
      "46:\tlearn: 1.1350276\ttotal: 1.76s\tremaining: 6.37s\n",
      "47:\tlearn: 1.1341757\ttotal: 1.79s\tremaining: 6.3s\n",
      "48:\tlearn: 1.1333010\ttotal: 1.82s\tremaining: 6.24s\n",
      "49:\tlearn: 1.1323349\ttotal: 1.85s\tremaining: 6.17s\n",
      "50:\tlearn: 1.1314203\ttotal: 1.87s\tremaining: 6.1s\n",
      "51:\tlearn: 1.1306057\ttotal: 1.9s\tremaining: 6.03s\n",
      "52:\tlearn: 1.1297756\ttotal: 1.93s\tremaining: 5.97s\n",
      "53:\tlearn: 1.1292982\ttotal: 1.97s\tremaining: 5.94s\n",
      "54:\tlearn: 1.1285356\ttotal: 1.99s\tremaining: 5.87s\n",
      "55:\tlearn: 1.1278726\ttotal: 2.03s\tremaining: 5.83s\n",
      "56:\tlearn: 1.1272483\ttotal: 2.06s\tremaining: 5.77s\n",
      "57:\tlearn: 1.1266236\ttotal: 2.1s\tremaining: 5.77s\n",
      "58:\tlearn: 1.1260765\ttotal: 2.15s\tremaining: 5.76s\n",
      "59:\tlearn: 1.1255513\ttotal: 2.2s\tremaining: 5.77s\n",
      "60:\tlearn: 1.1250913\ttotal: 2.24s\tremaining: 5.73s\n",
      "61:\tlearn: 1.1245852\ttotal: 2.28s\tremaining: 5.7s\n",
      "62:\tlearn: 1.1241243\ttotal: 2.31s\tremaining: 5.65s\n",
      "63:\tlearn: 1.1234508\ttotal: 2.34s\tremaining: 5.59s\n",
      "64:\tlearn: 1.1230308\ttotal: 2.37s\tremaining: 5.54s\n",
      "65:\tlearn: 1.1225518\ttotal: 2.4s\tremaining: 5.5s\n",
      "66:\tlearn: 1.1222374\ttotal: 2.44s\tremaining: 5.47s\n",
      "67:\tlearn: 1.1219675\ttotal: 2.47s\tremaining: 5.42s\n",
      "68:\tlearn: 1.1214077\ttotal: 2.5s\tremaining: 5.36s\n",
      "69:\tlearn: 1.1211153\ttotal: 2.53s\tremaining: 5.31s\n",
      "70:\tlearn: 1.1207310\ttotal: 2.56s\tremaining: 5.26s\n",
      "71:\tlearn: 1.1204555\ttotal: 2.59s\tremaining: 5.21s\n",
      "72:\tlearn: 1.1202592\ttotal: 2.62s\tremaining: 5.16s\n",
      "73:\tlearn: 1.1200747\ttotal: 2.64s\tremaining: 5.11s\n",
      "74:\tlearn: 1.1196050\ttotal: 2.69s\tremaining: 5.08s\n",
      "75:\tlearn: 1.1192958\ttotal: 2.72s\tremaining: 5.04s\n",
      "76:\tlearn: 1.1190390\ttotal: 2.75s\tremaining: 5s\n",
      "77:\tlearn: 1.1188114\ttotal: 2.78s\tremaining: 4.95s\n",
      "78:\tlearn: 1.1185113\ttotal: 2.8s\tremaining: 4.9s\n",
      "79:\tlearn: 1.1183134\ttotal: 2.83s\tremaining: 4.84s\n",
      "80:\tlearn: 1.1181194\ttotal: 2.85s\tremaining: 4.79s\n",
      "81:\tlearn: 1.1178438\ttotal: 2.88s\tremaining: 4.75s\n",
      "82:\tlearn: 1.1177045\ttotal: 2.92s\tremaining: 4.71s\n",
      "83:\tlearn: 1.1174441\ttotal: 2.95s\tremaining: 4.67s\n",
      "84:\tlearn: 1.1172821\ttotal: 2.97s\tremaining: 4.61s\n",
      "85:\tlearn: 1.1169763\ttotal: 3s\tremaining: 4.57s\n",
      "86:\tlearn: 1.1167736\ttotal: 3.03s\tremaining: 4.52s\n",
      "87:\tlearn: 1.1166217\ttotal: 3.05s\tremaining: 4.48s\n",
      "88:\tlearn: 1.1163197\ttotal: 3.08s\tremaining: 4.43s\n",
      "89:\tlearn: 1.1160739\ttotal: 3.11s\tremaining: 4.38s\n",
      "90:\tlearn: 1.1158271\ttotal: 3.14s\tremaining: 4.34s\n",
      "91:\tlearn: 1.1157490\ttotal: 3.17s\tremaining: 4.31s\n",
      "92:\tlearn: 1.1156468\ttotal: 3.2s\tremaining: 4.27s\n",
      "93:\tlearn: 1.1155459\ttotal: 3.23s\tremaining: 4.23s\n",
      "94:\tlearn: 1.1152441\ttotal: 3.26s\tremaining: 4.19s\n",
      "95:\tlearn: 1.1150624\ttotal: 3.29s\tremaining: 4.14s\n",
      "96:\tlearn: 1.1149444\ttotal: 3.31s\tremaining: 4.1s\n",
      "97:\tlearn: 1.1147264\ttotal: 3.35s\tremaining: 4.06s\n",
      "98:\tlearn: 1.1145713\ttotal: 3.38s\tremaining: 4.03s\n",
      "99:\tlearn: 1.1144462\ttotal: 3.41s\tremaining: 3.98s\n",
      "100:\tlearn: 1.1143962\ttotal: 3.43s\tremaining: 3.94s\n",
      "101:\tlearn: 1.1142021\ttotal: 3.46s\tremaining: 3.9s\n",
      "102:\tlearn: 1.1140991\ttotal: 3.49s\tremaining: 3.86s\n",
      "103:\tlearn: 1.1139756\ttotal: 3.52s\tremaining: 3.82s\n",
      "104:\tlearn: 1.1137994\ttotal: 3.55s\tremaining: 3.78s\n",
      "105:\tlearn: 1.1136506\ttotal: 3.58s\tremaining: 3.75s\n",
      "106:\tlearn: 1.1135712\ttotal: 3.61s\tremaining: 3.71s\n",
      "107:\tlearn: 1.1134696\ttotal: 3.64s\tremaining: 3.67s\n",
      "108:\tlearn: 1.1133588\ttotal: 3.66s\tremaining: 3.63s\n",
      "109:\tlearn: 1.1132026\ttotal: 3.69s\tremaining: 3.59s\n",
      "110:\tlearn: 1.1131206\ttotal: 3.71s\tremaining: 3.55s\n",
      "111:\tlearn: 1.1130526\ttotal: 3.75s\tremaining: 3.51s\n",
      "112:\tlearn: 1.1129345\ttotal: 3.77s\tremaining: 3.47s\n",
      "113:\tlearn: 1.1127885\ttotal: 3.81s\tremaining: 3.44s\n",
      "114:\tlearn: 1.1126390\ttotal: 3.85s\tremaining: 3.41s\n",
      "115:\tlearn: 1.1125183\ttotal: 3.88s\tremaining: 3.38s\n",
      "116:\tlearn: 1.1124494\ttotal: 3.91s\tremaining: 3.34s\n",
      "117:\tlearn: 1.1123293\ttotal: 3.93s\tremaining: 3.3s\n",
      "118:\tlearn: 1.1122014\ttotal: 3.96s\tremaining: 3.26s\n",
      "119:\tlearn: 1.1120444\ttotal: 3.99s\tremaining: 3.23s\n",
      "120:\tlearn: 1.1119681\ttotal: 4.02s\tremaining: 3.19s\n",
      "121:\tlearn: 1.1116968\ttotal: 4.05s\tremaining: 3.16s\n",
      "122:\tlearn: 1.1115919\ttotal: 4.08s\tremaining: 3.12s\n",
      "123:\tlearn: 1.1114363\ttotal: 4.11s\tremaining: 3.08s\n",
      "124:\tlearn: 1.1112120\ttotal: 4.14s\tremaining: 3.05s\n",
      "125:\tlearn: 1.1110960\ttotal: 4.17s\tremaining: 3.01s\n",
      "126:\tlearn: 1.1110141\ttotal: 4.2s\tremaining: 2.97s\n",
      "127:\tlearn: 1.1108890\ttotal: 4.22s\tremaining: 2.94s\n",
      "128:\tlearn: 1.1107591\ttotal: 4.26s\tremaining: 2.9s\n",
      "129:\tlearn: 1.1106818\ttotal: 4.28s\tremaining: 2.87s\n",
      "130:\tlearn: 1.1105121\ttotal: 4.31s\tremaining: 2.83s\n",
      "131:\tlearn: 1.1104587\ttotal: 4.33s\tremaining: 2.79s\n",
      "132:\tlearn: 1.1103053\ttotal: 4.36s\tremaining: 2.75s\n",
      "133:\tlearn: 1.1101547\ttotal: 4.39s\tremaining: 2.72s\n",
      "134:\tlearn: 1.1100346\ttotal: 4.42s\tremaining: 2.68s\n",
      "135:\tlearn: 1.1099105\ttotal: 4.45s\tremaining: 2.65s\n",
      "136:\tlearn: 1.1097940\ttotal: 4.48s\tremaining: 2.61s\n",
      "137:\tlearn: 1.1097189\ttotal: 4.5s\tremaining: 2.58s\n",
      "138:\tlearn: 1.1096097\ttotal: 4.53s\tremaining: 2.54s\n",
      "139:\tlearn: 1.1094715\ttotal: 4.56s\tremaining: 2.51s\n",
      "140:\tlearn: 1.1093244\ttotal: 4.59s\tremaining: 2.47s\n",
      "141:\tlearn: 1.1091885\ttotal: 4.61s\tremaining: 2.44s\n",
      "142:\tlearn: 1.1091327\ttotal: 4.64s\tremaining: 2.4s\n",
      "143:\tlearn: 1.1090998\ttotal: 4.66s\tremaining: 2.36s\n",
      "144:\tlearn: 1.1090620\ttotal: 4.69s\tremaining: 2.33s\n",
      "145:\tlearn: 1.1089332\ttotal: 4.73s\tremaining: 2.3s\n",
      "146:\tlearn: 1.1088526\ttotal: 4.76s\tremaining: 2.27s\n",
      "147:\tlearn: 1.1087852\ttotal: 4.79s\tremaining: 2.23s\n",
      "148:\tlearn: 1.1086169\ttotal: 4.82s\tremaining: 2.2s\n",
      "149:\tlearn: 1.1084935\ttotal: 4.84s\tremaining: 2.16s\n",
      "150:\tlearn: 1.1084449\ttotal: 4.87s\tremaining: 2.13s\n",
      "151:\tlearn: 1.1083218\ttotal: 4.91s\tremaining: 2.1s\n",
      "152:\tlearn: 1.1081983\ttotal: 4.94s\tremaining: 2.07s\n",
      "153:\tlearn: 1.1081066\ttotal: 4.97s\tremaining: 2.03s\n",
      "154:\tlearn: 1.1079483\ttotal: 5s\tremaining: 2s\n",
      "155:\tlearn: 1.1079055\ttotal: 5.02s\tremaining: 1.96s\n",
      "156:\tlearn: 1.1078354\ttotal: 5.05s\tremaining: 1.93s\n",
      "157:\tlearn: 1.1077183\ttotal: 5.08s\tremaining: 1.9s\n",
      "158:\tlearn: 1.1075688\ttotal: 5.1s\tremaining: 1.86s\n",
      "159:\tlearn: 1.1074610\ttotal: 5.14s\tremaining: 1.83s\n",
      "160:\tlearn: 1.1069329\ttotal: 5.17s\tremaining: 1.8s\n",
      "161:\tlearn: 1.1068055\ttotal: 5.2s\tremaining: 1.76s\n",
      "162:\tlearn: 1.1067488\ttotal: 5.23s\tremaining: 1.73s\n",
      "163:\tlearn: 1.1066720\ttotal: 5.25s\tremaining: 1.7s\n",
      "164:\tlearn: 1.1065946\ttotal: 5.28s\tremaining: 1.66s\n",
      "165:\tlearn: 1.1064432\ttotal: 5.32s\tremaining: 1.63s\n",
      "166:\tlearn: 1.1063953\ttotal: 5.34s\tremaining: 1.6s\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "167:\tlearn: 1.1062818\ttotal: 5.37s\tremaining: 1.57s\n",
      "168:\tlearn: 1.1062013\ttotal: 5.4s\tremaining: 1.53s\n",
      "169:\tlearn: 1.1061313\ttotal: 5.43s\tremaining: 1.5s\n",
      "170:\tlearn: 1.1060538\ttotal: 5.46s\tremaining: 1.47s\n",
      "171:\tlearn: 1.1059258\ttotal: 5.48s\tremaining: 1.43s\n",
      "172:\tlearn: 1.1058122\ttotal: 5.51s\tremaining: 1.4s\n",
      "173:\tlearn: 1.1057440\ttotal: 5.54s\tremaining: 1.37s\n",
      "174:\tlearn: 1.1056701\ttotal: 5.57s\tremaining: 1.34s\n",
      "175:\tlearn: 1.1056172\ttotal: 5.6s\tremaining: 1.3s\n",
      "176:\tlearn: 1.1055823\ttotal: 5.63s\tremaining: 1.27s\n",
      "177:\tlearn: 1.1055694\ttotal: 5.66s\tremaining: 1.24s\n",
      "178:\tlearn: 1.1055238\ttotal: 5.68s\tremaining: 1.21s\n",
      "179:\tlearn: 1.1054852\ttotal: 5.71s\tremaining: 1.17s\n",
      "180:\tlearn: 1.1053483\ttotal: 5.74s\tremaining: 1.14s\n",
      "181:\tlearn: 1.1052243\ttotal: 5.77s\tremaining: 1.11s\n",
      "182:\tlearn: 1.1051745\ttotal: 5.79s\tremaining: 1.07s\n",
      "183:\tlearn: 1.1050523\ttotal: 5.82s\tremaining: 1.04s\n",
      "184:\tlearn: 1.1049349\ttotal: 5.85s\tremaining: 1.01s\n",
      "185:\tlearn: 1.1048598\ttotal: 5.87s\tremaining: 979ms\n",
      "186:\tlearn: 1.1047920\ttotal: 5.9s\tremaining: 946ms\n",
      "187:\tlearn: 1.1047098\ttotal: 5.92s\tremaining: 914ms\n",
      "188:\tlearn: 1.1046558\ttotal: 5.95s\tremaining: 882ms\n",
      "189:\tlearn: 1.1045573\ttotal: 5.98s\tremaining: 849ms\n",
      "190:\tlearn: 1.1045019\ttotal: 6s\tremaining: 817ms\n",
      "191:\tlearn: 1.1043735\ttotal: 6.03s\tremaining: 786ms\n",
      "192:\tlearn: 1.1042346\ttotal: 6.06s\tremaining: 754ms\n",
      "193:\tlearn: 1.1041200\ttotal: 6.09s\tremaining: 722ms\n",
      "194:\tlearn: 1.1040206\ttotal: 6.12s\tremaining: 691ms\n",
      "195:\tlearn: 1.1039718\ttotal: 6.15s\tremaining: 659ms\n",
      "196:\tlearn: 1.1038191\ttotal: 6.17s\tremaining: 626ms\n",
      "197:\tlearn: 1.1037724\ttotal: 6.2s\tremaining: 595ms\n",
      "198:\tlearn: 1.1037245\ttotal: 6.22s\tremaining: 563ms\n",
      "199:\tlearn: 1.1035852\ttotal: 6.25s\tremaining: 532ms\n",
      "200:\tlearn: 1.1035552\ttotal: 6.28s\tremaining: 500ms\n",
      "201:\tlearn: 1.1035432\ttotal: 6.31s\tremaining: 468ms\n",
      "202:\tlearn: 1.1034732\ttotal: 6.34s\tremaining: 437ms\n",
      "203:\tlearn: 1.1034116\ttotal: 6.37s\tremaining: 406ms\n",
      "204:\tlearn: 1.1033832\ttotal: 6.39s\tremaining: 374ms\n",
      "205:\tlearn: 1.1033051\ttotal: 6.42s\tremaining: 343ms\n",
      "206:\tlearn: 1.1029874\ttotal: 6.45s\tremaining: 312ms\n",
      "207:\tlearn: 1.1029296\ttotal: 6.48s\tremaining: 280ms\n",
      "208:\tlearn: 1.1028231\ttotal: 6.5s\tremaining: 249ms\n",
      "209:\tlearn: 1.1027989\ttotal: 6.53s\tremaining: 218ms\n",
      "210:\tlearn: 1.1027987\ttotal: 6.55s\tremaining: 186ms\n",
      "211:\tlearn: 1.1026700\ttotal: 6.58s\tremaining: 155ms\n",
      "212:\tlearn: 1.1022369\ttotal: 6.61s\tremaining: 124ms\n",
      "213:\tlearn: 1.1020912\ttotal: 6.64s\tremaining: 93ms\n",
      "214:\tlearn: 1.1020186\ttotal: 6.67s\tremaining: 62ms\n",
      "215:\tlearn: 1.1018690\ttotal: 6.7s\tremaining: 31ms\n",
      "216:\tlearn: 1.1016899\ttotal: 6.73s\tremaining: 0us\n",
      "Wall time: 6.95 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Pipeline(steps=[('ml',\n",
       "                 <catboost.core.CatBoostRegressor object at 0x0000021B0006BC10>)])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# обучаем обновленную модель на всей обучающей выборке\n",
    "final_ml_pipe.fit(\n",
    "                fulldata[FTS_COLS], \n",
    "                y_fulldata.clip(lower=1.0e-200).apply(lambda x: math.log(x+1, BASE_LOG ))\n",
    "                )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "explicit-rating",
   "metadata": {
    "papermill": {
     "duration": 0.085876,
     "end_time": "2021-04-25T11:10:34.645302",
     "exception": false,
     "start_time": "2021-04-25T11:10:34.559426",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "dill.dump(final_ml_pipe, file = open(\"pipe_CATBOOST_1.cbm\", \"wb\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "dff92fd7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting predict.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile predict.py\n",
    "\n",
    "import pathlib\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from catboost import CatBoostRegressor\n",
    "import dill\n",
    "import math\n",
    "BASE_LOG = 4 # math.e\n",
    "\n",
    "MODEL_FILE = pathlib.Path(__file__).parent.joinpath(\"pipe_CATBOOST_1.cbm\")\n",
    "\n",
    "AGG_COLS = [\"material_code\", \"company_code\", \"country\", \"region\", \"manager_code\"]\n",
    "FTS_COLS = ['material_code', 'company_code', 'region', 'month', 'vol_tm1', 'country_grp_mean_10', \n",
    "             'avg_2', 'avg_3', 'avg_12', 'median2', 'median3', 'median4', 'median12', 'max_2', 'max_3']\n",
    "TARGET = \"target\"\n",
    "\n",
    "# \n",
    "def get_features(df: pd.DataFrame, month: pd.Timestamp, N=1) -> pd.DataFrame:\n",
    "    \"\"\"Calculate features for `month`.\"\"\"\n",
    "\n",
    "    start_period = month - pd.offsets.MonthBegin(N)\n",
    "    end_period = month - pd.offsets.MonthBegin(1)\n",
    "\n",
    "    df = df.loc[:, :end_period]\n",
    "\n",
    "    features = pd.DataFrame([], index=df.index)\n",
    "    features[\"month\"] = month.month\n",
    "    # формируем лаги за N месяцев\n",
    "    features[[f\"vol_tm{i}\" for i in range(N, 0, -1)]] = df.loc[:, start_period:end_period].copy()\n",
    " \n",
    "    # Добавление ГРУППОВЫХ скользящих средних\n",
    "    gr = \"country\"\n",
    "    period = 10\n",
    "    df2 = df.copy()\n",
    "    df2[df2.columns.to_list()] = \\\n",
    "                           df2.groupby(level=gr).transform(lambda x: x.mean())\n",
    "    grp_manager_roll_mean = df2.rolling(period, axis=1, min_periods=1)\n",
    "    features = \\\n",
    "        features.join( grp_manager_roll_mean.mean().iloc[:, -1].rename(gr+\"_grp_mean_\"+str(period)))\n",
    "\n",
    "    #  MEAN\n",
    "    for period in [2,3,12]: #range(2,13,1):\n",
    "        rolling = df.rolling(period, axis=1, min_periods=1)\n",
    "        features = features.join(rolling.mean().iloc[:, -1].rename(\"avg_\"+str(period)))\n",
    "\n",
    "    #  median\n",
    "    for period in [2,3,4,12]: # 10,11,12]: #range(2,13,1):\n",
    "        rolling = df.rolling(period, axis=1, min_periods=1)\n",
    "        features = features.join(rolling.median().iloc[:, -1].rename(\"median\"+str(period)))\n",
    "                 \n",
    "    #  MAX\n",
    "    for period in range(2,4,1):\n",
    "        rolling = df.rolling(period, axis=1, min_periods=1)\n",
    "        features = features.join(rolling.max().iloc[:, -1].rename(\"max_\"+str(period)))\n",
    "\n",
    "    features[\"month\"] = month.month\n",
    "\n",
    "    return features.reset_index()\n",
    "\n",
    "\n",
    "def predict(df: pd.DataFrame, month: pd.Timestamp) -> pd.DataFrame:\n",
    "\n",
    "    model = dill.load(open(MODEL_FILE, \"rb\"))\n",
    "\n",
    "    group_ts = df.groupby(AGG_COLS + [\"month\"])[\"volume\"].sum().unstack(fill_value=0)\n",
    "    \n",
    "    tmp = group_ts.clip(lower=1.0e-200).copy()\n",
    "    for i in tmp.columns:\n",
    "        tmp[i] = tmp[i].apply(lambda x: math.log(x+1, BASE_LOG))\n",
    "    \n",
    "    features = get_features(tmp, month)\n",
    "\n",
    "    for c in features.columns:\n",
    "        col_type = features[c].dtype\n",
    "        if col_type == 'object': \n",
    "            features[c] = features[c].astype('category')    \n",
    "    \n",
    "    predictions = model.predict(features[FTS_COLS])\n",
    "    predictions = BASE_LOG**predictions - 1\n",
    "    \n",
    "    predictions = pd.Series(predictions).clip(lower=0, upper=7000)\n",
    "\n",
    "    preds_df = features[AGG_COLS].copy()\n",
    "    preds_df[\"prediction\"] = predictions\n",
    "    return preds_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "177e90bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<module 'predict' from 'C:\\\\Users\\\\dimacv\\\\PROJECTS\\\\Соревнования\\\\Sibur2021\\\\predict.py'>"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import predict\n",
    "import importlib\n",
    "importlib.reload(predict)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "f9b7064b",
   "metadata": {},
   "source": [
    "predict.get_features(group_ts.iloc[:, :-1], pd.Timestamp(\"2020-07-01\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "49476129",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>material_code</th>\n",
       "      <th>company_code</th>\n",
       "      <th>country</th>\n",
       "      <th>region</th>\n",
       "      <th>manager_code</th>\n",
       "      <th>prediction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>124</td>\n",
       "      <td>7278</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Респ. Татарстан</td>\n",
       "      <td>17460</td>\n",
       "      <td>0.821424</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>Минская обл.</td>\n",
       "      <td>10942</td>\n",
       "      <td>154.608452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>Могилевская обл.</td>\n",
       "      <td>10942</td>\n",
       "      <td>5.735792</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Белоруссия</td>\n",
       "      <td>г. Минск</td>\n",
       "      <td>10942</td>\n",
       "      <td>2.122394</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>133</td>\n",
       "      <td>0</td>\n",
       "      <td>Казахстан</td>\n",
       "      <td>г. Нур-Султан</td>\n",
       "      <td>13301</td>\n",
       "      <td>15.705467</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>936</th>\n",
       "      <td>986</td>\n",
       "      <td>9943</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Смоленская обл.</td>\n",
       "      <td>17460</td>\n",
       "      <td>40.436204</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>937</th>\n",
       "      <td>998</td>\n",
       "      <td>0</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Ленинградская обл.</td>\n",
       "      <td>18079</td>\n",
       "      <td>2.753668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>938</th>\n",
       "      <td>998</td>\n",
       "      <td>3380</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Ленинградская обл.</td>\n",
       "      <td>14956</td>\n",
       "      <td>32.392293</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>939</th>\n",
       "      <td>998</td>\n",
       "      <td>5410</td>\n",
       "      <td>Россия</td>\n",
       "      <td>г. Санкт-Петербург</td>\n",
       "      <td>14956</td>\n",
       "      <td>77.239490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>940</th>\n",
       "      <td>998</td>\n",
       "      <td>6346</td>\n",
       "      <td>Россия</td>\n",
       "      <td>Респ. Башкортостан</td>\n",
       "      <td>10737</td>\n",
       "      <td>14.510163</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>941 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     material_code  company_code     country              region  \\\n",
       "0              124          7278      Россия     Респ. Татарстан   \n",
       "1              133             0  Белоруссия        Минская обл.   \n",
       "2              133             0  Белоруссия    Могилевская обл.   \n",
       "3              133             0  Белоруссия            г. Минск   \n",
       "4              133             0   Казахстан       г. Нур-Султан   \n",
       "..             ...           ...         ...                 ...   \n",
       "936            986          9943      Россия     Смоленская обл.   \n",
       "937            998             0      Россия  Ленинградская обл.   \n",
       "938            998          3380      Россия  Ленинградская обл.   \n",
       "939            998          5410      Россия  г. Санкт-Петербург   \n",
       "940            998          6346      Россия  Респ. Башкортостан   \n",
       "\n",
       "     manager_code  prediction  \n",
       "0           17460    0.821424  \n",
       "1           10942  154.608452  \n",
       "2           10942    5.735792  \n",
       "3           10942    2.122394  \n",
       "4           13301   15.705467  \n",
       "..            ...         ...  \n",
       "936         17460   40.436204  \n",
       "937         18079    2.753668  \n",
       "938         14956   32.392293  \n",
       "939         14956   77.239490  \n",
       "940         10737   14.510163  \n",
       "\n",
       "[941 rows x 6 columns]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts_preds_tst = predict.predict(data[data.month<\"2020-07-01\"], pd.Timestamp(\"2020-07-01\"))\n",
    "ts_preds_tst "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "d691127e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ошибка на тестовом множестве: 1.5137\n"
     ]
    }
   ],
   "source": [
    "print(\"Ошибка на тестовом множестве:\",\n",
    "      f'{np.sqrt(mean_squared_log_error(group_ts.reset_index().iloc[:,-1], ts_preds_tst[\"prediction\"])):.4f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "3aa8717f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Windows\n",
    "!tar.exe -a -c -f Pip_CTB_NF_L4_laglog_11E.zip pipe_CATBOOST_1.cbm requirements.txt predict.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "20583948",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Время работы скрипта:: 0.0 hours 0.0 minutes 29.01949429512024 seconds.\n"
     ]
    }
   ],
   "source": [
    "end_time = time()\n",
    "time_taken = end_time - start_time # time_taken is in seconds\n",
    "hours, rest = divmod(time_taken,3600)\n",
    "minutes, seconds = divmod(rest, 60)\n",
    "print('Время работы скрипта:: {} hours {} minutes {} seconds.'.format(hours, minutes, seconds))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58b4094a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 1175.250436,
   "end_time": "2021-04-25T11:10:37.128577",
   "environment_variables": {},
   "exception": null,
   "input_path": "__notebook__.ipynb",
   "output_path": "__notebook__.ipynb",
   "parameters": {},
   "start_time": "2021-04-25T10:51:01.878141",
   "version": "2.3.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
